-
Notifications
You must be signed in to change notification settings - Fork 17.7k
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
slices: add iterator-related functions #61899
Comments
Concat takes a a variadic second arg #56353 |
@rsc – Any reason why |
I think this makes sense here. The iters package would define the iter.Iter type and things like Map, Filter, TakeWhile, and Zip that don't deal with particular iterator sources. You import slices when you want to convert an iter.Iter to/from a slice, and e.g. maps.Keys when you want to iterate over a map. |
// Better name pending.
func SecondRet[T1, T2 any](iter Seq2[T1, T2]) Seq[T2] {
return func(yield func(T2) bool) {
for _, v := range iter {
if !yield(v) {
return false
}
}
}
} Either that or Also, two more functions that I'd like to suggest: // SortInto places the elements of iter into the already sorted slice dst, expanding as necessary, and returns the resulting slice, also sorted.
func SortInto[S ~[]E, E cmp.Ordered](dst S, seq Seq[E]) S
func SortIntoFunc[S ~[]E, E any](dst S, seq Seq, cmp func(E, E) int) And |
This proposal has been added to the active column of the proposals project |
I’m a little nervous looking at this that someone is going to immediately release a fluent API for these which will be preferred by the community, and STL will be left with a less used package. E.g
and so on. Maybe it’s ok if that ends up happening, but seems a bit sad. |
That’s both comforting and distressing :) |
Please add syntax highlighting to your code blocks. With it, you might have noticed that Sorted should be SortedFunc here: // SortedFunc collects values from seq into a new slice, sorts the slice, and returns it.
func Sorted[Elem any](seq iter.Seq[Elem], cmp func(Elem, Elem) int) []Elem { It seems like the functions that return iter types should be in the iter package, so they can be grouped under the Seq/Seq2 types in the generated documentation. These are technically "constructors" for those types in that sense. The functions that take iter types also seem like they belong in iter, as utility functions that work on iter types. I'm reminded of the io package, which declares Reader and various functions that produce and consume Readers. Slices are built into the language, so it doesn't seem like a function producing or consuming a slice alone warrants it being in the slices package. The new go/token.File.Lines method returns a slice, yet I doubt it was ever considered whether it belonged in the slices package (being a method aside). |
I dont mind the colorless code blocks. The tone there also seems a bit rude @willfaught |
My apologies. |
In most language, the func Iter[E any](slice []E) iter.Seq[E] {
...
}
// and for enumerate
func Enumerate[E any](slice []E) iter.Seq2[int, E] {
// or if we had tuple type, return should be iter.Seq[(int, E)]
// Again, I personally think that Seq2 is a very unwise design
...
} |
I think you're forgetting the context. The function isn't |
I had not paid close attention to the names of the functions this proposal wants to add so far. In #61626 (comment), @rsc gives an example use as follows.
Somehow knowing that I think that my initial confusion when reading the call site without having invested in already knowing the details of the |
@ChrisHines I think it's fairly common for a function to be named after what it returns and what it does, but less so after what it takes. There is also precedence: In python, To me, this seems fine. |
Besides Python, JavaScript has Array.toSorted which returns a new Array that was sorted. So, two languages at least where "sort" means in place and "sorted" means "new slice/list/array". |
Does Sorted need a stable variant? It does seem unfortunate to have so many sort variants:
cc @eliben |
Python's |
func Sorted[Elem comparable](seq iter.Seq[Elem]) []Elem {
slice := Collect(seq)
Sort(slice)
return slice
} I'm not a sorting expert, but I'm curious if using an online sorting algorithm here could be an improvement. There could be delays between iterations. |
The heap package is in need of a total overhaul. It could incorporate iterators if that happened. |
Maybe slices.SortedFunc should be stable by default. Anyone who wants a faster sort can write it manually, and stability bugs are more likely to matter in production than minor performance hits. |
Change https://go.dev/cl/568477 mentions this issue: |
I suspect there is a typo in the API above. |
Should AppendSeq have a vet check according to #62729? |
My intuition may not be good here, but I don't see why people would think that they can ignore the result of |
They should know once they are past the beginner stage, but the compiler actually does not allow ignoring the result of
|
Change https://go.dev/cl/595515 mentions this issue: |
For #61899 Change-Id: I3586b9b59e87159d21e1a270dabb3af213592739 Reviewed-on: https://go-review.googlesource.com/c/go/+/595515 Auto-Submit: Ian Lance Taylor <iant@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Alan Donovan <adonovan@google.com> Reviewed-by: Ian Lance Taylor <iant@google.com>
Does |
For golang#61899 Change-Id: I3586b9b59e87159d21e1a270dabb3af213592739 Reviewed-on: https://go-review.googlesource.com/c/go/+/595515 Auto-Submit: Ian Lance Taylor <iant@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Alan Donovan <adonovan@google.com> Reviewed-by: Ian Lance Taylor <iant@google.com>
Some late, Is it better to rename |
|
Okay. Though personally, I think |
Late to the party, but should a declaration like this: func Collect[Elem any](seq iter.Seq[Elem]) []Elem instead be: func Collect[Elem any, Seq ~func(func(Elem) bool)](seq Seq) []Elem This way the caller isn't confined to using the The same comment applies in a few other places too. |
@bobg It's possible to assign a value of type |
Hm, I'm not sure I can. The closest I can come is to imagine that someone will want their own named type for specific kinds of sequence, maybe something like: type MySeq[T constraints.Integer] func(func (T) bool) It will be annoying in a case like this always to have to type-convert with But I concede this is pretty hypothetical. |
We propose to add the following functions to package slices, to provide good support for code using iterators.
This is one of a collection of proposals updating the standard library for the new 'range over function' feature (#61405). It would only be accepted if that proposal is accepted. See #61897 for a list of related proposals.
All serves as a “source” for iterators.
Backward can be used to replace existing 3-clause loops that iterate backward over a slice manually. This happens fairly often in certain algorithms (for example it happens a lot in the compiler’s SSA code).
Values is like All: not terribly useful by itself but useful as a source for other code.
Append and Collect serve as “sinks” for iterators.
Sorted and SortedFunc collect an iterator and then sort the contents:
The text was updated successfully, but these errors were encountered: