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
slices provides slices.Contains() and slices.ContainsFunc(), both are convenient for determining wether a slice contains a certain element. That's great but seems not enough for common scene that need filter some matched elements in slices and do something with them. Based on this, I want to introduce two new APIs MatchedThen and MatchedFuncThen into slices/iter.
Proposal
I propose adding the following APIs to the slices package as a part of slices/iter.go.
// MatchedThen returns an iterator that yields the clements matched.
func MatchedThen[S ~[]E, E comparable](s S, v E) iter.Seq[E] {...}
// MatchedFuncThen returns an iterator that yields the clements of s satisfies f(e).
func MatchedFuncThen[S ~[]E, E comparable](s S, f func(E) bool) iter.Seq[E] {...}
Both MatchedThen and MatchedFuncThen are based on iterator, we can easily use it according to our preference, pull or push mode.
s1 := []int{1, 2, 3, 3, 4, 5}
for v := range slices.MatchedThen(s1, 3) {
fmt.Println(v)
}
// Output:
// 3
// 3
Iter.Seq[E] was chosen instead of iter.Seq2[E] because the resulting index after filtering would be confusing which looks discontinuous. And users usually focus on the objects themselves in the match, not the index.
The text was updated successfully, but these errors were encountered:
I think this is a dup of #61898, particularly its Filter function. Please reopen if you disagree.
Agree with Filter function, it can be used not only for slices, but also for all iterable objects such as map, array, slice, etc, which provide iter. But if you need a dedicated filter capability used in the slices library, just like slices.All(), slices.Values(), please let me know and I will reopen this issue.
Background
slices
providesslices.Contains()
andslices.ContainsFunc()
, both are convenient for determining wether a slice contains a certain element. That's great but seems not enough for common scene that need filter some matched elements in slices and do something with them. Based on this, I want to introduce two new APIsMatchedThen
andMatchedFuncThen
intoslices/iter
.Proposal
I propose adding the following APIs to the
slices
package as a part ofslices/iter.go
.Both
MatchedThen
andMatchedFuncThen
are based on iterator, we can easily use it according to our preference, pull or push mode.Design discussion
Why iter.Seq[E] and not iter.Seq2[E]?
Iter.Seq[E] was chosen instead of iter.Seq2[E] because the resulting index after filtering would be confusing which looks discontinuous. And users usually focus on the objects themselves in the match, not the index.
The text was updated successfully, but these errors were encountered: