MCPcopy Index your code
hub / github.com/adam-hanna/arrayOperations / Reduce

Function Reduce

arrayOperations.go:38–46  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

36// fmt.Println(Reduce[string, map[string]int](arr, countAnimals, initialState))
37// // output: map["cat":2 "dog":1 "cow":1]
38func 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//

Callers 1

TestReduceFunction · 0.85

Calls

no outgoing calls

Tested by 1

TestReduceFunction · 0.68

Used in the wild real call sites across dependent graphs

searching dependent graphs…