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: Go 2: add Reverse function to new slices package #47988

Closed
DeedleFake opened this issue Aug 26, 2021 · 1 comment
Closed

proposal: Go 2: add Reverse function to new slices package #47988

DeedleFake opened this issue Aug 26, 2021 · 1 comment

Comments

@DeedleFake
Copy link

DeedleFake commented Aug 26, 2021

I wanted to comment about this in #47203, but it's been locked, unfortunately. I needed a reverse function for something so I did a manual loop with the intention of replacing it with a call to slices.Reverse() when Go 1.18 comes out, but to my surprise it doesn't seem to have even been proposed. The only equivalent in the standard library that I saw was sort.Reverse(), but that's really inefficient, and a lot more complicated to use, than just a simple reverse.

The proposal's very simple:

package slices

func Reverse[T any](s []T) {
  for i := 0; i < len(s) / 2; i++ {
    r := len(s) - i - 1
    s[i], s[r] = s[r], s[i]
  }
}
@gopherbot gopherbot added this to the Proposal milestone Aug 26, 2021
@DeedleFake
Copy link
Author

But #45955 is still open. I'll post this there instead. Sorry about the extra issue.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants