-
Notifications
You must be signed in to change notification settings - Fork 17.5k
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: x/exp/slices: add Sorted #56692
Comments
original would be b := slices.Clone(a)
slices.Sort(b) imo this is clear enough |
|
The name Sorted is inspired from python |
One line is clearer and more flixable than 3. consider this: // instead of
a := functionReturnsArray()
slices.Sort(a)
passToFunction(a)
// can be reduced to
passToFunction(slices.Sorted(functionReturnsArray())) |
Another common scenario. a_clone := slices.Clone(a)
b_clone := slices.Clone(b)
slices.Sort(a_clone)
slices.Sort(b_clone)
diff := cmp.Diff(a_clone, b_clone) with Sorted diff := cmp.Diff(slices.Sorted(a), slices.Sorted(b)) Think also if you want to compare more than 2. |
Seems like folks don't need this feature. |
You can retract the proposal if you like, but note that the proposal process takes weeks even at its fastest. This proposal hadn't even made it to the committee. There are hundreds of proposals in the backlog, unfortunately; see https://github.com/orgs/golang/projects/17. But I agree that the emoji voting on this proposal is not in favor. |
Will keep this open for now to give it complete shot, and the committee can decide. |
No one is arguing that slices.Sorted is not useful. The main problem from a Go API design standpoint is that it hides an allocation that the user has no control over, which makes programs that use slices.Sorted generate more garbage than they might otherwise appear to. In contrast, if you write slices.Clone first, then it's more obvious what is going on. |
Perfect, Thank you for explaining your point of view, Always appreciate a proper discussion :). It is a good point and I agree with it, But also I don't think a function shouldn't be implemented just because it might not be obvious for some user, We could come up with better ways to make it obvious for them that this function allocates an extra space, those ways could be name changing, comments on the function, highlighting it on the docs, .. etc. |
The function could be called |
This proposal has been added to the active column of the proposals project |
Based on the discussion above, this proposal seems like a likely decline. |
No change in consensus, so declined. |
It would be great to have a sorted, generic function similar to Sort but instead of sorting in place it returns a new sorted array.
Usage:
In my opinion the second approach is cleaner, more readable and easier to understand.
The text was updated successfully, but these errors were encountered: