Total Lines | 13 |
Duplicated Lines | 0 % |
Changes | 0 |
1 | package gotil |
||
2 | |||
3 | // Reduce iterates given collection and returns the accumulated result of running each element |
||
4 | // the last param is initial value of accumulator. |
||
5 | // result := Reduce([]int{5, 10}, func(accumulator, v, i int) int { |
||
6 | // return accumulator + v |
||
7 | // }, 0) |
||
8 | // fmt.Println(result) |
||
9 | // // Output: 15 |
||
10 | func Reduce[T any, A any](s []T, f func(accumulator A, v T, i int) A, accumulator A) A { |
||
11 | if len(s) < 1 { |
||
12 | return accumulator |
||
13 | } |
||
14 | for i, v := range s { |
||
19 |