-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevery.go
More file actions
34 lines (31 loc) · 810 Bytes
/
every.go
File metadata and controls
34 lines (31 loc) · 810 Bytes
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
package gotil
// EveryBy checks if predicate returns truthy for each elements of given collection. Iteration is stopped once predicate returns false
func EveryBy[T any](s []T, f func(item T) bool) bool {
for _, v := range s {
if !f(v) {
return false
}
}
return true
}
// Contains checks if given item is exists in given collection. Iteration is stopped once predicate returns true
// result := gotil.Contains([]int{5, 10, 15}, 10)
// fmt.Println(result)
// // Output: true
func Contains[T comparable](s []T, item T) bool {
for _, v := range s {
if v == item {
return true
}
}
return false
}
// ContainsBy checks return true if given predicate return true.
func ContainsBy[T any](s []T, f func(item T) bool) bool {
for _, v := range s {
if f(v) {
return true
}
}
return false
}