Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

proposal: x/exp/xiter: add Compact{,Func}{,2} #67441

Open
jimmyfrasche opened this issue May 16, 2024 · 0 comments
Open

proposal: x/exp/xiter: add Compact{,Func}{,2} #67441

jimmyfrasche opened this issue May 16, 2024 · 0 comments
Labels
Milestone

Comments

@jimmyfrasche
Copy link
Member

jimmyfrasche commented May 16, 2024

Add

func Compact[E comparable](iter.Seq[E]) iter.Seq[E]

func CompactFunc[E any](iter.Seq[E], func(E, E) bool) iter.Seq[E]

(and similarly for the iter.Seq2 variants) to x/exp/xiter.

These are the iter equivalent of slices.Compact{,Func}.

In #61898, @rsc said:

There was a suggestion to add Compact and CompactFunc, analogous to slices.Compact and slices.CompactFunc. Those seem less important for Seqs than slices, since they are intended for use after slices.Sort, and there is no xiter.Sort, nor can there be. (At that point, code might as well use slices.Collect+slices.Sort.) Filing a separate proposal for these would make sense, to separate that discussion.

There are two reasons to prefer an iterator version for slices

  1. you need to keep the original with duplicates but want to walk it without
  2. you no longer need the slice and could avoid moving its contents around by deduplicating online

There are nonslice uses as well

  1. the input is coming from some kind of process that is sorted but may contain runs of duplicates (DB results, output of a forked process, etc.)
  2. the input is unsorted but you can still ignore runs of duplicates safely
  3. you're mapping the values of a seq in a way that can cause runs of duplicates in the output that weren't in the input
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: Incoming
Development

No branches or pull requests

1 participant