-
Notifications
You must be signed in to change notification settings - Fork 11
/
some.go
43 lines (34 loc) · 815 Bytes
/
some.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package fp
// Determines whether the specified callback function returns true for any element of an array.
func Some[T any](predicate func(T) bool) func([]T) bool {
return func(xs []T) bool {
for _, x := range xs {
if predicate(x) {
return true
}
}
return false
}
}
// See Some but callback receives index of element.
func SomeWithIndex[T any](predicate func(T, int) bool) func([]T) bool {
return func(xs []T) bool {
for i, x := range xs {
if predicate(x, i) {
return true
}
}
return false
}
}
// Like Some but callback receives index of element and the whole array.
func SomeWithSlice[T any](predicate func(T, int, []T) bool) func([]T) bool {
return func(xs []T) bool {
for i, x := range xs {
if predicate(x, i, xs) {
return true
}
}
return false
}
}