-
Notifications
You must be signed in to change notification settings - Fork 17.6k
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 Swap
and Swapper
functions
#63169
Comments
I'm not opposed to this. Still, given that this is restricted to slices, how often do we expect people to implement |
This could be made more general by taking pointers instead: func Swap[E any](v1, v2 *E) { *v1, *v2 = *v2, *v1 } Then you could do func (ex *Example) Swap(i1, i2 int) {
pkg.Swap(&ex.s[i1], &ex.s[i2])
} Ah, beat me to it, @ianlancetaylor. Your reply appeared as I was typing this. |
sort.Data is more general than slices.Sort. For example, it allows sorting when your records are organized in columns. |
That's much noisier than |
slices.Swap would also be useful for using rand.Shuffle. E.g., https://pkg.go.dev/math/rand#example-Shuffle-SlicesInUnison |
Swap
functionSwap
and Swapper
functions
I added a |
I propose adding to package slices (or package sort):
This is a common operation when implementing
sort.Interface
(e.g., appears 164 times within $GOROOT). It's not hard to write out by hand, but it's tedious writings
4 times when it's a long expression, or making sure to get the i/j/j/i ordering right.The text was updated successfully, but these errors were encountered: