Completed
Push — master ( a1ea93...617d0b )
by Hayrullah
14s queued 13s
created

filter.go   A

Size/Duplication

Total Lines 15
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
dl 0
loc 15
rs 10
c 0
b 0
f 0
1
package gotil
2
3
// FilterBy iterates over elements of collection, returning an array of all elements predicate if returns true for.
4
// 	result := gotil.FilterBy([]int64{-100, -5, 30, 100}, func(v int64, i int) bool {
5
// 	return v > 0
6
// 	})
7
// 	fmt.Println(result)
8
// 	// Output: [30 100]
9
func FilterBy[T any](s []T, f func(v T, i int) bool) []T {
10
	result := []T{}
11
	for i, v := range s {
12
		if f(v, i) {
13
			result = append(result, v)
14
		}
15
	}
16
	return result
17
}
18