FindOne iterates through an array applying the guard function to each element and returns the first element that passes. If no such element is found, it returns the zero value and false. arr := []int{1,2,3,4} func isEven(i int) bool { return i % 2 == 0 } fmt.Println(FindOne[int](arr, isEven)
(arr []T, guard func(T) bool)
| 10 | // fmt.Println(FindOne[int](arr, isEven)) |
| 11 | // // output: [2] |
| 12 | func FindOne[T any](arr []T, guard func(T) bool) (T, bool) { |
| 13 | for idx := range arr { |
| 14 | if guard(arr[idx]) { |
| 15 | return arr[idx], true |
| 16 | } |
| 17 | } |
| 18 | |
| 19 | return *new(T), false |
| 20 | } |
| 21 | |
| 22 | // Reduce iterates through an array applying the function to each element and the cumulative value and the final state of the cumulative value |
| 23 | // |
searching dependent graphs…