Passed
Pull Request — master (#93)
by Hayrullah
02:09
created

every.go   A

Size/Duplication

Total Lines 8
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
package gotil
2
3
// EveryBy checks if predicate returns truthy for each elements of given collection. Iteration is stopped once predicate returns false
4
func EveryBy[T any](s []T, f func(item T) bool) bool {
5
	for _, v := range s {
6
		if !f(v) {
7
			return false
8
		}
9
	}
10
	return true
11
}
12
13
// Contains checks if given item is exists in given collection. Iteration is stopped once predicate returns true
14
//	result := gotil.Contains([]int{5, 10, 15}, 10)
15
//	fmt.Println(result)
16
//	// Output: true
17
func Contains[T comparable](s []T, item T) bool {
18
	for _, v := range s {
19
		if v == item {
20
			return true
21
		}
22
	}
23
	return false
24
}
25
26
// ContainsBy checks return true if given predicate return true.
27
func ContainsBy[T any](s []T, f func(item T) bool) bool {
28
	for _, v := range s {
29
		if f(v) {
30
			return true
31
		}
32
	}
33
	return false
34
}
35