-
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
x/exp/slices: add Replace #55358
Comments
I often want a
|
This proposal has been added to the active column of the proposals project |
@go101 I suggested slices.Concat during some phase of this process. :-) slices.Concat is still my preferred spelling for this. |
|
Given that we have Delete and Insert, Replace seems like it belongs. |
Replace()
It sounds like people are in favor of:
|
Based on the discussion above, this proposal seems like a likely accept. |
It looks |
Should I open a separate issue for Concat? |
This issue is about Replace. Feel free to open a separate issue about Concat. Votes alone are not really the metric either - Replace fits next to the existing Insert and Delete. |
No change in consensus, so accepted. 🎉 |
Change https://go.dev/cl/444682 mentions this issue: |
At a glance, it looks like the implementation above could allocate twice because of the double Edit: The implementation at the time of writing: t := s[j:]
s = append(s[:i], v...)
s = append(s, t...)
return s |
Probably better to take that to the CL comments. |
I have on multiple occasions had a need to replace elements of a slice. This is easily accomplished by calling
slices.Delete()
followed byslices.Insert()
, but this results in more copying than is necessary. Well, unless the compiler's capable of eliminating the extra work involved, but I have no idea if it can or not.Regardless, my proposal's simple: Add the following function to the slices package:
This would function exactly like
except that it would be a bit more efficient.
The text was updated successfully, but these errors were encountered: