Reduce iterates through an array applying the function to each element and the cumulative value and the final state of the cumulative value arr := []string{"cat","dog","cat","cow"} func countAnimals(state map[string]int, animal string) map[string]int { count, ok := state[animal] if !ok {
(arr []T, fn func(A, T) A, init A)
| 36 | // fmt.Println(Reduce[string, map[string]int](arr, countAnimals, initialState)) |
| 37 | // // output: map["cat":2 "dog":1 "cow":1] |
| 38 | func Reduce[T, A any](arr []T, fn func(A, T) A, init A) A { |
| 39 | ret := init |
| 40 | |
| 41 | for idx := range arr { |
| 42 | ret = fn(ret, arr[idx]) |
| 43 | } |
| 44 | |
| 45 | return ret |
| 46 | } |
| 47 | |
| 48 | // Filter iterates through an array applying the guard function to each element and returns the elements that pass |
| 49 | // |
no outgoing calls
searching dependent graphs…