Total Lines | 15 |
Duplicated Lines | 0 % |
Changes | 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 |
||
18 |