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
maps: add iterator-related functions #61900
Comments
This proposal has been added to the active column of the proposals project |
Finishing this proposal discussion is blocked on #61405. |
Change https://go.dev/cl/558736 mentions this issue: |
It is unfortunate that |
@earthboundkid ISTM if that is a concern, it should maybe rather be I get, FWIW, that "get the sorted keys from a map" is a fairly common use case, to try and get deterministic order from the specifically non-deterministic maps. But it still seems a more general problem. |
maps.Keys could return a sequence implementation with a length hint and slices.Sorted could type assert for that, similar to what some io.Reader functions do. |
Nevermind, iter.Seq is a concrete type, not an interface. |
It's true that slices.Sorted(maps.Keys(m)) will not pre-size the slice, but that's not necessarily a strike against the iterator forms. We could have a separate discussion about maps.KeysSorted and maps.KeysSortedFunc as optimizations, if that became a concern. |
Have all remaining concerns about this proposal been addressed? The full godoc is:
|
SortedKeys can wait and be a separate proposal. |
More wordy, but |
Based on the discussion above, this proposal seems like a likely accept. The full godoc is:
|
What, if anything, is guaranteed about the iteration order of Keys and Values (EDIT: and All)? I believe this should be specified in the docs. Does these two for loops yield the same key order? I would assume not, based on analogy to range-over-map.
What about this?
I can see why it might make sense from an implementation standpoint for this to also be randomized. However it also seems weird for a value of type Seq, which nominally represents a sequence, to in fact yield multiple different sequences. |
|
@earthboundkid An iter.Seq is not an iterator and does not exhaust. |
Yes, I deleted my comment but you beat me to it. 😄 |
Happy to update docs to say that iteration order is undefined (different each time) and Insert does overwrite keys. |
No change in consensus, so accepted. 🎉 The full godoc is:
|
@rsc I don't see any doc changes |
We propose to add the following functions to package maps, 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.
Keys and Values are like All: not terribly useful by themselves but useful as inputs to other iteration adapters.
In particular, we expect that
x := slices.Sorted(maps.Keys(m))
will be a common pattern.Insert and Collect serve as “sinks” for iterators.
The text was updated successfully, but these errors were encountered: