-
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
proposal: slices: add Shuffle #61612
Comments
Thanks. You said what, but not why. When would this be used? See also https://go.dev/doc/faq#x_in_std . |
I've used shuffle a few times but only ever with slices. This feels like it should be a method on One option to simplify using package slices
func Swapper[S ~[]E, E any](slice S) func(i, j int) {
return func(i, j int) {
s[i], s[j] = s[j], s[i]
})
} But since package slices will have all those handy sorting functions the only real place that I can think of using this is with Shuffle. |
@ianlancetaylor Updated the issue. I tried to submit this issue to https://github.com/golang/exp because I thought that place is for experimental things, but I saw that the |
This proposal has been added to the active column of the proposals project |
It seems like a level inversion to me to have slices import rand. Perhaps rand/v2 (#61716) should be handling this instead. There is an active discussion about that over there. |
This proposal is a duplicate of a previously discussed proposal, as noted above, |
Why?
Every time I use
Shuffle
, I have to writeswap
function like this and the form of it isn't likely to change:Also, there's a chance to make a typo, like:
s[i], s[j] = s[i], s[i]
.I think this is a good example to use generic. We also have
slices.Sort
, which feels like a shortcut tosort.Slice
to me, so I thought it makes sense to haveslices.Shuffle
too.Proposal
Add
Shuffle
function like this:We can also consider some alternatives:
math/rand
insteadinterface { Shuffle(int, func(int, int)) }
instead of*rand.Rand
The text was updated successfully, but these errors were encountered: