Skip to content

Commit b7be74a

Browse files
committed
[collection] Added a Match function
1 parent ee4e027 commit b7be74a

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

changes/20251013111319.feature

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
:sparkles: `[collection]` Added a `Match` function

utils/collection/search.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,15 @@ func Reject[S ~[]E, E any](s S, f FilterFunc[E]) S {
128128
return Filter(s, func(e E) bool { return !f(e) })
129129
}
130130

131+
// Match checks whether an element e matches any of the matching functions.
132+
func Match[E any](e E, matches ...FilterFunc[E]) bool {
133+
conditions := NewConditions(len(matches))
134+
for i := range matches {
135+
conditions.Add(matches[i](e))
136+
}
137+
return conditions.Any()
138+
}
139+
131140
type ReduceFunc[T1, T2 any] func(T2, T1) T2
132141

133142
// Reduce runs a reducer function f over all elements in the array, in ascending-index order, and accumulates them into a single value.

utils/collection/search_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,18 @@ func TestFilterReject(t *testing.T) {
138138
}))
139139
}
140140

141+
func TestMatch(t *testing.T) {
142+
match1 := func(i int) bool { return i == 1 }
143+
match2 := func(i int) bool { return i == 2 }
144+
match3 := func(i int) bool { return i == 3 }
145+
assert.True(t, Match(1, match1, match2, match3))
146+
assert.True(t, Match(2, match1, match2, match3))
147+
assert.True(t, Match(3, match1, match2, match3))
148+
assert.False(t, Match(4, match1, match2, match3))
149+
assert.False(t, Match(0, match1, match2, match3))
150+
assert.False(t, Match(2, match1, match3))
151+
}
152+
141153
func TestMap(t *testing.T) {
142154
mapped := Map([]int{1, 2}, func(i int) string {
143155
return fmt.Sprintf("Hello world %v", i)

0 commit comments

Comments
 (0)