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: maps: add maps.KeysSlice and maps.ValuesSlice #61626

Closed
cuiweixie opened this issue Jul 28, 2023 · 19 comments
Closed

proposal: maps: add maps.KeysSlice and maps.ValuesSlice #61626

cuiweixie opened this issue Jul 28, 2023 · 19 comments
Labels
Milestone

Comments

@cuiweixie
Copy link
Contributor

cuiweixie commented Jul 28, 2023

Since we need to reserve Keys and Values for iterators.
I think we might need to change the old maps.Keys/Values to maps.KeysSlice/ValuesSlice as mention in 513715

@AndrewHarrisSPU
Copy link

Does anyone like CloneKeys() []K, CloneValues() []V?

I'm not sure I'm not bike-shedding, but Clone might communicate the allocation of the returned slice, and the shallow copy semantics. slices uses Clone to mean shallow copy.

@andig
Copy link
Contributor

andig commented Aug 4, 2023

Does anyone like CloneKeys() []K, CloneValues() []V?

It does not feel necessary. It's (almost) obvious that a map type object does not have its key and values in slices.

@rsc
Copy link
Contributor

rsc commented Aug 9, 2023

#61900 adds Keys and Values back as iterators.
#61899 adds slices.Collect and slices.Sorted.

With these, the old maps.Keys(m) would become slices.Collect(maps.Keys(m)) and similarly Values.
Better, this idiom:

keys := maps.Keys(m)
slices.Sort(keys)
use(keys)

becomes

use(slices.Sorted(maps.Keys(m))

Are there other pattern uses for maps.Keys (other than sorting them) that we should be sure to cover?

@earthboundkid
Copy link
Contributor

Just my 2¢, but yeah, I feel like as long as slices.Sorted gets added, the uses of maps.KeySlice are infrequent enough that it could just be keys := slices.Append(make([]K,0, len(m)), maps.Keys(m)) those times when you need it.

@rsc
Copy link
Contributor

rsc commented Aug 9, 2023

This proposal has been added to the active column of the proposals project
and will now be reviewed at the weekly proposal review meetings.
— rsc for the proposal review group

@willfaught
Copy link
Contributor

Are there other pattern uses for maps.Keys (other than sorting them) that we should be sure to cover?

I suppose creating a set of them using a map[K]struct{}.

@earthboundkid
Copy link
Contributor

You could do that with an iterator and InsertInto.

@rsc
Copy link
Contributor

rsc commented Aug 30, 2023

Finishing this proposal discussion is blocked on #61405.

@gopherbot
Copy link

Change https://go.dev/cl/535215 mentions this issue: runtime: use real type size in map keys and values functions

gopherbot pushed a commit that referenced this issue Oct 14, 2023
We were using the size stored in the map, which is the smaller
of the real type size and 128.

As of CL 61538 we don't use these functions, but we expect to
use them again in the future after #61626 is resolved.

Change-Id: I7bfb4af5f0e3a56361d4019a8ed7c1ec59ff31fd
Reviewed-on: https://go-review.googlesource.com/c/go/+/535215
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Keith Randall <khr@golang.org>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Auto-Submit: Ian Lance Taylor <iant@google.com>
Reviewed-by: Keith Randall <khr@google.com>
yunginnanet pushed a commit to yunginnanet/go that referenced this issue Oct 20, 2023
We were using the size stored in the map, which is the smaller
of the real type size and 128.

As of CL 61538 we don't use these functions, but we expect to
use them again in the future after golang#61626 is resolved.

Change-Id: I7bfb4af5f0e3a56361d4019a8ed7c1ec59ff31fd
Reviewed-on: https://go-review.googlesource.com/c/go/+/535215
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Keith Randall <khr@golang.org>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Auto-Submit: Ian Lance Taylor <iant@google.com>
Reviewed-by: Keith Randall <khr@google.com>
@rsc rsc changed the title proposal: maps: add maps.KeysSlice and maps.ValuesSlice proposal: maps: add maps.KeysSlice and maps.ValuesSlice Oct 26, 2023
@rsc
Copy link
Contributor

rsc commented Jan 23, 2024

The key issue seems to be #61626 (comment).
It sounds like if we add maps.Keys, maps.Values, and slices.Collect, then we can use

KeySlices(m) => slices.Collect(maps.Keys(m)) (or slices.Sorted(maps.Keys(m)))
ValuesSlice(m) => slices.Collect(maps.Values(m))

and do not need to add these explicitly.

@rsc
Copy link
Contributor

rsc commented Feb 1, 2024

Based on the discussion above, this proposal seems like a likely decline.
— rsc for the proposal review group

@andig
Copy link
Contributor

andig commented Feb 1, 2024

and do not need to add these explicitly.

Need, no. Make devs life easier and Go more approachable yes.

@mibk
Copy link
Contributor

mibk commented Feb 5, 2024

I feel like the issue is #61900 (comment). I agree it's more elegant to write a single line instead of three, but I don't mind writing those extra lines for the sake of optimization.

@earthboundkid
Copy link
Contributor

I think it’s a little too soon to permanently decline these. I think we definitely can skip them for now, but it may be that after a few years of experience there turns out to be some reason they are needed after all and this can be reopened then.

@earthboundkid
Copy link
Contributor

@Nasfame See also #61900.

@rsc
Copy link
Contributor

rsc commented Feb 7, 2024

I think it’s a little too soon to permanently decline these.

Decline is never permanent. Only accept is permanent.

@rsc
Copy link
Contributor

rsc commented Feb 8, 2024

No change in consensus, so declined.
— rsc for the proposal review group

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: Declined
Development

No branches or pull requests

9 participants