-
Notifications
You must be signed in to change notification settings - Fork 11
/
every.go
43 lines (34 loc) · 805 Bytes
/
every.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 all the members of an array satisfy the specified test.
func Every[T any](predicate func(T) bool) func([]T) bool {
return func(xs []T) bool {
for _, x := range xs {
if !predicate(x) {
return false
}
}
return true
}
}
// See Every but callback receives index of element.
func EveryWithIndex[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 false
}
}
return true
}
}
// Like Every but callback receives index of element and the whole array.
func EveryWithSlice[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 false
}
}
return true
}
}