-
Notifications
You must be signed in to change notification settings - Fork 18k
proposal: maps: add analogues to Keys/Values that return slices #72909
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
Another option would be to add a |
To be sure, I am ambivalent about my own proposal. On one hand, this is such a straightforward function to implement that it might not be worth expanding API surface for. On the other hand, I generally think it's a good idea for the stdlib to make it easy to do the right thing. |
This would still have some overhead from the repeated function calls involved in iterators. A pre-allocated version of |
Related Issues
(Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.) |
@DeedleFake what about something like AppendCollect where you can pass the destination slice? |
That already exists under the name |
Proposal Details
If a program requires a slice of a map's keys or values, there are some options:
slices.Collect(maps.Values(m))
(which has unnecessary allocations).slices.AppendSeq(make([]K, 0, len(m)), maps.Values(m))
(or, more realistically, write this as a function). Edited to add: Even better, you'd write a function that iterates over the map manually instead of calling the iter-based methods at all.Proposal: add standard library functions (maybe call them
maps.KeysSlice
ormaps.ValuesSlice
) to return a slice of map keys or values respectively.The text was updated successfully, but these errors were encountered: