-
Notifications
You must be signed in to change notification settings - Fork 17.9k
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: CopyFunc and AppendFunc #71277
Comments
As with all such additions it would be helpful to have numbers to see how common the issue is. It would be hard to classify all code that may be written in terms of one of these as they are quite general. Personally I've written a lot of code like for i := range s {
s[i] = f(s2[i])
} and for _, v := range s {
s2 = append(s2, f(v))
} where While there are a few variants of each to match against even with those, I think those would be a good proxy. They'd be a common case so they'd represent some large fraction of the true value. |
I think I'd rather see
than
I think for 2 major reasons:
|
1 comes from the Copy part of the name and 2 comes from the Func part of the name. |
That requires remembering which direction the
It's hard to know exactly given just the name, without having to go read the docs. Without delving into the types of things one could imagine |
Generally the function name would make it clear, as with Maybe you would have to check the docs sometimes. That's fine. That's why they are there. I don't think these are especially compelling arguments as they could be applied to lots of other things that already exist. |
fwiw, the original names were either |
Proposal Details
The
copy
andappend
builtins are well known and useful. Combining copy and append with elementwise mapping yields two useful functions that are easy to explain in existing terms:CopyFunc
is defined similar tocopy
but takes an additional mapping function to apply before assigning the src element to dst.The implementation for
AppendFunc
is simple and straightforward enough that I include it in full:AppendFunc
could have the signature(dst S, f func(E2) E1, src ...E2) S
. Unlike lowercaseappend
, it is always for working with a full slice—otherwise you could have just usedappend(s, f(v))
. It also makes it easier to pass an anonymous mapping function when it's the last parameter.Note that in-place map is
CopyFunc(s, s, f)
and new-slice map isAppendFunc([]T(nil), s, f)
.AppendFunc
could be written with existing functions (assumingxiter
):This is more awkward and unlikely to be as efficient.
CopyFunc
would require a correspondingCopySeq
and would not be able to handle certain overlaps between dst and src correctly.The text was updated successfully, but these errors were encountered: