Skip to content

Commit e17971d

Browse files
authored
[collection] Added a Match function (#722)
<!-- Copyright (C) 2020-2022 Arm Limited or its affiliates and Contributors. All rights reserved. SPDX-License-Identifier: Apache-2.0 --> ### Description - added "match" functions to simplify matching assessments ### Test Coverage <!-- Please put an `x` in the correct box e.g. `[x]` to indicate the testing coverage of this change. --> - [x] This change is covered by existing or additional automated tests. - [ ] Manual testing has been performed (and evidence provided) as automated testing was not feasible. - [ ] Additional tests are not required for this change (e.g. documentation update).
1 parent ee4e027 commit e17971d

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-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: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,24 @@ 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+
func match[E any](e E, matches []FilterFunc[E]) *Conditions {
132+
conditions := NewConditions(len(matches))
133+
for i := range matches {
134+
conditions.Add(matches[i](e))
135+
}
136+
return &conditions
137+
}
138+
139+
// Match checks whether an element e matches any of the matching functions.
140+
func Match[E any](e E, matches ...FilterFunc[E]) bool {
141+
return match[E](e, matches).Any()
142+
}
143+
144+
// MatchAll checks whether an element e matches all the matching functions.
145+
func MatchAll[E any](e E, matches ...FilterFunc[E]) bool {
146+
return match[E](e, matches).All()
147+
}
148+
131149
type ReduceFunc[T1, T2 any] func(T2, T1) T2
132150

133151
// 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: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,20 @@ 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+
assert.True(t, MatchAll(1, match1))
152+
assert.False(t, MatchAll(1, match1, match2))
153+
}
154+
141155
func TestMap(t *testing.T) {
142156
mapped := Map([]int{1, 2}, func(i int) string {
143157
return fmt.Sprintf("Hello world %v", i)

0 commit comments

Comments
 (0)