Filter iterates through an array applying the guard function to each element and returns the elements that pass arr := []int{0,1,2,3,4} func isEven(i int) bool { return i % 2 == 0 } fmt.Println(Filter[int](arr, isEven)) // output: [0,2,4]
(arr []T, guard func(T) bool)
| 54 | // fmt.Println(Filter[int](arr, isEven)) |
| 55 | // // output: [0,2,4] |
| 56 | func Filter[T any](arr []T, guard func(T) bool) []T { |
| 57 | var ret []T |
| 58 | |
| 59 | for idx := range arr { |
| 60 | if guard(arr[idx]) { |
| 61 | ret = append(ret, arr[idx]) |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | return ret |
| 66 | } |
| 67 | |
| 68 | // Map iterates through an array applying the transform function to each element and returns the modified array |
| 69 | // |
searching dependent graphs…