You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
So I propose to add a simple function "CloneFunc". "CloneFunc" may look like this:
// CloneFunc copies any elements from s for which f returns truefuncCloneFunc[S~[]E, Eany](sS, ffunc(E) bool) S {
ret:=make([]E, 0, len(s)) // Maybe it's not worth doing thisfori:=ranges {
iff(s[i]) {
ret=append(ret, s[i])
}
}
returnret
}
"CloneFunc" may not be a good name, but it conforms to the naming rules of the slices APIs. Maybe calling it "Filter" would make more sense.
Maybe it is simpler to use loops directly in many cases, but I think it is better to add common patterns to the slices standard library, which can also reduce the difficulty of switching from other languages to golang.
I'm happy to contribute a pull request with tests if this is considered a good idea.
The text was updated successfully, but these errors were encountered:
The conventional name for this operation is filter. The name CloneFunc makes me think of map. And FWIW, both filter and map are special cases of foldMap.
Proposal Details
I found that there is a common pattern when copying data from slices:
I did a simple scan of the "golang/go" code base, and found more than 30 places of this pattern. For examples:
So I propose to add a simple function "CloneFunc". "CloneFunc" may look like this:
There are similar functions in other languages:
I personally think "CloneFunc" can make the code simpler, taking "net/lookup.go" as an example:
"CloneFunc" may not be a good name, but it conforms to the naming rules of the slices APIs. Maybe calling it "Filter" would make more sense.
Maybe it is simpler to use loops directly in many cases, but I think it is better to add common patterns to the slices standard library, which can also reduce the difficulty of switching from other languages to golang.
I'm happy to contribute a pull request with tests if this is considered a good idea.
The text was updated successfully, but these errors were encountered: