-
Notifications
You must be signed in to change notification settings - Fork 18k
proposal: x/exp/maps: add Entries #54012
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
Comments
I'm skeptical about this. If we do this, maps.Entry[K, V] is going to end up all over everyone's code base as a generic pair type (in code that has nothing to do with maps). |
How about something that allows the user to use their own type via a function, such as func EntriesFunc[K comparable, V any, M ~map[K]V, P any](m M, f func(K, V) P) (entries []P) {
entries = make([]P, 0, len(m))
for k, v := range m {
entries = append(entries, f(k, v))
}
return entries
} It seems a bit less useful to me, but it could still be useful in certain circumstances. |
@DeedleFake I feel like we're staying away from that kind of map/filter/reduce functions until we get a handle on iterators. |
If the goal is to collect it into a slice, the question of whether it'll need a predefined |
That feels like unnecessary fear of what could happen. It would be more concerning if |
The comment says the API would ease this pattern:
but I don't see what happens next in this pattern. Is the code going to return a []maps.Entry? Why is that better than a more specific type? It's unclear why code would ever want to have a variable with type maps.Entry or []maps.Entry instead of something more specific. |
This proposal has been added to the active column of the proposals project |
@rsc, the main use for this pattern in the code I work with is iteration of maps in a defined (here, sorted) order. So some kind of |
|
@fzipp, thanks, but I've known about that idiom and had already used it in the past. The issue with that one is that it iterates over a map twice, whereas an entries version would only need one iteration, since the values would be grouped together with the keys. |
@ainar-g In the
Or are you now talking about this hypothetical |
I meant map iterations in particular. Also, this is getting a bit off-topic, as my first comment, responding to rsc, was an example of a possible use of |
Based on the discussion above, this proposal seems like a likely decline. |
No change in consensus, so declined. |
There are helper functions to extract the keys and the values, but not one to extract both together.
I propose adding:
I often find myself sorting a map based on the value, but needing the key and value paired together.
This API would ease that pattern:
The text was updated successfully, but these errors were encountered: