-
Notifications
You must be signed in to change notification settings - Fork 17.8k
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
slices: add Reverse #58565
Comments
Change https://go.dev/cl/468855 mentions this issue: |
What are the use cases for reverse? This has come up a lot in criticisms of go but I find that in my day job I rarely want for it. the only time I recall using a reverse function was for swapping sort order (which it was useful for). Wondering if there might be others I’m not thinking of. |
It comes up whenever a function produces a slice whose elements are in the opposite order. ;-) I too find it's not something that comes up a lot in my own code, but that's usually because I control the producer and consumer of ordered slices and I can usually make them agree on a sensible order. But still it comes up, and it's a very clearly defined operation. A few examples quickly found in GOROOT:
and in x/tools:
|
Imagine you have a big slice, and a function that returns a slice of indexes form the big slice which have to be deleted. If you do sth like: bigSlice := []any{...}
indexesToDelete := []int{ ... }
for _, indexToDelete := range indexesToDelete {
bigSlice = append(bigSlice[:indexToDelete], bigSlice[indexToDelete+1:]...)
} you will have a bug, because the following indexes are now pointing to the wrong elements. There are two easy ways to fix this:
I just have run into this reviewing a PR 😅 Proof of what I say: |
@jmwielandt Why would you rather move memory around with
|
That sounds like "why would you use 'range' instead of a good ol' for loop" to me.
|
@jmwielandt For iteration there are other better ideas like user-defined iteration using range over func values, so I would say that use case arguments for a memory moving slices.Reverse function should not be based on simple reverse iteration needs. |
I have a Trie implementation where I needed a Reverse function. // Query returns all matched values along the key path.
// The returned elements are sorted in order of proximity to the key.
// The first element in the slice is the closest value to the key.
func (t *Trie[T]) Query(key string) []Entry[T] {
ee := []Entry[T]{}
if t.root.hasValue {
ee = append(ee, Entry[T]{Value: t.root.value})
}
n := &t.root
for i, r := range key {
var ok bool
if n, ok = n.children[r]; !ok {
break
}
if n.hasValue {
ee = append(ee, Entry[T]{
Key: key[:i+1],
Value: n.value,
})
}
}
common.Reverse(ee)
return ee
} |
This proposal has been added to the active column of the proposals project |
Based on the discussion above, this proposal seems like a likely accept. |
We should also add a double-reverse function, for those times when a single reverse just is not enough.... |
The compiler already inserts calls to its highly optimized implementation of double-reverse in all the necessary places. |
I suggest using double ROT13 for this.
|
Closed before accepted? |
Oh, sorry, I now saw the comment in the CL: https://go-review.googlesource.com/c/go/+/468855/9#message-fc275fcf37f3207e30d99afe18cb2cc5c9686e28 |
No change in consensus, so accepted. 🎉 |
Fixes golang#58565 Change-Id: I583f8380c12386178fb18e553322bbb019d9fae0 Reviewed-on: https://go-review.googlesource.com/c/go/+/468855 Run-TryBot: Alan Donovan <adonovan@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@google.com> Reviewed-by: Shay Nehmad <dude500@gmail.com>
Fixes golang#58565 Change-Id: I583f8380c12386178fb18e553322bbb019d9fae0 Reviewed-on: https://go-review.googlesource.com/c/go/+/468855 Run-TryBot: Alan Donovan <adonovan@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@google.com> Reviewed-by: Shay Nehmad <dude500@gmail.com>
During the review of the proposal for the new
slices
package I mentioned that Reverse, a common slice operation, seemed to be missing, and was advised to file a separate proposal. So here it is. I propose we add this function:You can see the full implementation in https://go.dev/cl/468855. ;-)
The text was updated successfully, but these errors were encountered: