proposal: slices: new package to provide generic slice functions (discussion) #47203
Replies: 22 comments 163 replies
-
[Resolved: Left as
I had proposed I think I'd prefer to leave those functions out entirely. It seems clear from the debate over the naming and functionality that there isn't really a canonical definition of uniqueness that everyone can agree belongs in the package. |
Beta Was this translation helpful? Give feedback.
-
[Resolved as Grow(n) and Clip. -rsc, July 21 2021] Copying over my last comment to track discussion on the
|
Beta Was this translation helpful? Give feedback.
-
[Resolved: Removed from API. -rsc, July 21 2021] (From #45955 (comment).) I still think that the functional parts of the API (
In #45955 (comment), I pointed out that coming up a lot in discussions is not the usual bar that we apply for additions to the Go standard library. I don't think that concern has been addressed, but again I may have missed it in the long discussion threads. I would really like to see examples of proposed usage of these functions — especially as compared to equivalent |
Beta Was this translation helpful? Give feedback.
-
[Resolved: Added Clip to API. -rsc, July 21 2021] I think the discussion from #45955 (comment) was also lost in the noise. My original comment:
@ianlancetaylor in #45955 (comment):
My reply in #45955 (comment):
|
Beta Was this translation helpful? Give feedback.
-
[Resolved: Decided to leave this out - IndexFunc suffices for now. -rsc, July 21 2021] This got lost in the original thread but I've often found these useful: func Any[T any](vs []T, p func(T) bool) bool {
for _, v := range vs {
if p(v) {
return true
}
}
return false
}
func All[T any](vs []T, p func(T) bool) bool {
for _, v := range vs {
if !p(v) {
return false
}
}
return true
}
js calls them some/every but I want to say that any/all are the more common names, though I haven't done a survey. |
Beta Was this translation helpful? Give feedback.
-
[Resolved: Deleted ContainsFunc for now. -rsc, July 21 2021]
I would rather see the more generic form of // ContainsFunc is like Contains, but it uses a comparison function.
func ContainsFunc[T any](s []T, f func(T) bool) bool And still reads well at call sites: var t []time.Time
ok := slices.ContainsFunc(t, time.Unix(1626299610).Equals)
ok = slices.ContainsFunc(t, time.Time.IsZero) |
Beta Was this translation helpful? Give feedback.
-
Linking again to my Google Sheet comparing names of Python/JavaScript slice methods: https://docs.google.com/spreadsheets/u/0/d/1SFHxz7u8ufnORtBpu63vRGCQSbyA3dqYHgz4eX6osas/htmlview |
Beta Was this translation helpful? Give feedback.
-
[Resolved: Added Clone. -rsc, July 28 2021] One function that seems natural to include is
(or it could be called What I would usually write today is
which isn't too bad, but is still a rather dense and busy line for such a basic operation. |
Beta Was this translation helpful? Give feedback.
-
[Resolved: We agree. -rsc, July 28 2021] Proposal: 1.18 should have a slices package that contains only a subset of functionality that has strong consensus for being useful. At the same time golang.org/x/exp/slices is released with all the same functions plus the more controversial ones (definitely Map and Filter, maybe others?). After a year or so, the controversial functions can get an up or down decision on inclusion in the standard library. |
Beta Was this translation helpful? Give feedback.
-
[Resolved: Decided not to add in the initial set. -rsc, July 28 2021] I’d like to see |
Beta Was this translation helpful? Give feedback.
-
Update, July 21 2021 GitHub Discussions seem to be working much better than regular issues. Based on the discussion, we have made the following changes to the proposed API:
I have also added [Resolved] notes at the top of comments that we consider resolved, in addition to replying with text like "Resolved as/by ...". (I hope GitHub will introduce the concept of a resolved comment at some point.) The suggestions about Clone and Concat are not resolved: we will do those next week. My guess is Clone will be added and Concat will not. |
Beta Was this translation helpful? Give feedback.
-
[Resolved: Decided not to add sequence operations. -rsc, July 28 2021] The new reduced API looks great! This may have gotten lost in the discussion in the original issue, so I'll risk repeating it: Should
Besides satisfying more use cases, this also maps more closley to what the strings and bytes packages already offer:
|
Beta Was this translation helpful? Give feedback.
-
[Resolved: Panics are OK when the corresponding language operations would panic. -rsc, July 28 2021] I prefer panics to be reserved for situations where it is not safe to continue. Are these functions using panics for optimising for performance and so we will have to wrap the stdlib functions (like I do for the errors package with a null checking version) where personally I believe panic handling to be itself, more of a risk than additional checks and error handling, considering the panic could be anything from the world and not just the path that you are expecting. Could panic free e.g. length checking alternatives be considered to be part of the package? |
Beta Was this translation helpful? Give feedback.
-
Do we want a maps package? A sets package? If so, is now the time to propose them, so they can be harmonized with slices? Edit: Resolved below. |
Beta Was this translation helpful? Give feedback.
-
[Resolved: Yes, we will allow named slice types. -rsc, July 28 2021] One thing we need to consider is whether to handle defined slice types. Specifically, if a function like func Insert[T any](s []T, i int, v ...T) []T Now consider: type Durations []time.Duration
func (s Durations) String() string { ... }
func PushAndPrint(s Durations, v time.Duration) {
// Insert will return []time.Duration, not Durations,
// so fmt.Println will not invoke the Durations.String method.
fmt.Println(slices.Insert(s, 0, v))
} The other approach is to use constraint type inference, which looks like this: func Insert[T any, S interface { []T }](s S, i int, v ...T) S This will do the right thing for the In the current proposal this applies to Thoughts? CC @griesemer |
Beta Was this translation helpful? Give feedback.
-
[Resolved: Will add to constraints proposal. -rsc, July 28 2021]
I definitely don't want people writing slices.Slice[T] instead of []T.
? |
Beta Was this translation helpful? Give feedback.
-
[Resolved: Will make EqualFunc take two different slice type parameters. -rsc, July 28 2021] I'll advocate for this one more time, since it's a bit hard to tell whether it was missed or decided against: The kinds of comparison loops that I often end up writing (or, at least, the ones that I notice) are often comparing a slice of keys against a slice of a compound type using a specific field (e.g. a list of IDs with a list of users) or primitive types against some kind of named type (e.g. strings and thrift-generated named strings). In tests, there are also cases where cmp and a Transformer is used to run these kinds of equality checks that could be simplified using a dual-typed EqualFunc. For these cases, I think that the I think this holds true for I don't think it's all wins, but I think the pros of making this change outweigh the cons:
Also, I suppose:
https://go2goplay.golang.org/p/82l-NfVxPui (includes naive implementation)
Original comments for more detail: |
Beta Was this translation helpful? Give feedback.
-
Update, July 28 2021 Based on the discussion, we will make the following changes to the proposed API:
I have also added [Resolved] notes at the top of comments that we consider resolved, in addition to replying with text like "Resolved...". @ianlancetaylor is going to update the proposal text. At this point I believe all the suggestions to date have been resolved and that we could potentially move the proposal to likely accept next week. |
Beta Was this translation helpful? Give feedback.
-
[Resolved: Will leave IndexFunc as is -rsc, August 4 2021] About -func IndexFunc[T any](s []T, f func(T) bool) int
+func IndexFunc[T any](s []T, f func(T, int) bool) int |
Beta Was this translation helpful? Give feedback.
-
Update, August 4 2021 There are no changes this week. One comment (about IndexFunc) was resolved in favor of the status quo. |
Beta Was this translation helpful? Give feedback.
-
Update, August 11 2021 The proposal #45955 has been accepted. |
Beta Was this translation helpful? Give feedback.
-
What's the procedure for adding more functions? I didn't see Reverse discussed anywhere. I just had to write it and was reminded of how tricky it can be. |
Beta Was this translation helpful? Give feedback.
-
This discussion is meant to be for collecting feedback about #45955 in a more structured way.
Please comment here, and when things seem resolved, we'll move back to the issue.
Update: The proposal has been accepted. I've locked the discussion since it is done. Thanks everyone!
Beta Was this translation helpful? Give feedback.
All reactions