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: slices: add Swap and Swapper functions #63169

Open
mdempsky opened this issue Sep 22, 2023 · 6 comments
Open

proposal: slices: add Swap and Swapper functions #63169

mdempsky opened this issue Sep 22, 2023 · 6 comments
Labels
Milestone

Comments

@mdempsky
Copy link
Member

mdempsky commented Sep 22, 2023

I propose adding to package slices (or package sort):

// Swap swaps s[i] and s[j].
func Swap[S ~[]E, E any](s S, i, j int) { s[i], s[j] = s[j], s[i] }

// Swapper returns a function that swaps s[i] and s[j].
func Swapper[S ~[]E, E any](s S) func(i, j int) { return func(i, j int) { Swap(s, i, j) } }

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 writing s 4 times when it's a long expression, or making sure to get the i/j/j/i ordering right.

@gopherbot gopherbot added this to the Proposal milestone Sep 22, 2023
@ianlancetaylor
Copy link
Contributor

I'm not opposed to this. Still, given that this is restricted to slices, how often do we expect people to implement sort.Interface in new code, as opposed to writing a comparison function that can be passed to slices.Sort?

@DeedleFake
Copy link

DeedleFake commented Sep 22, 2023

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.

@mdempsky
Copy link
Member Author

how often do we expect people to implement sort.Interface in new code, as opposed to writing a comparison function that can be passed to slices.Sort?

sort.Data is more general than slices.Sort. For example, it allows sorting when your records are organized in columns.

@mdempsky
Copy link
Member Author

pkg.Swap(&ex.s[i1], &ex.s[i2])

That's much noisier than slices.Swap(ex.S, i1, i2), IMO. It requires extra &, [, and ] tokens and requires repeating the ex.s expression.

@mdempsky
Copy link
Member Author

slices.Swap would also be useful for using rand.Shuffle. E.g., https://pkg.go.dev/math/rand#example-Shuffle-SlicesInUnison

@mdempsky mdempsky changed the title proposal: slices: add Swap function proposal: slices: add Swap and Swapper functions Sep 24, 2023
@mdempsky
Copy link
Member Author

I added a Swapper suggestion too, to further simplify the common case of just wanting to curry the first argument to Swap.

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

No branches or pull requests

4 participants