Passed
Pull Request — master (#83)
by ertugrul
02:10
created

reduce.go   A

Size/Duplication

Total Lines 13
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
dl 0
loc 13
rs 10
c 0
b 0
f 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 {
15
		accumulator = f(accumulator, v, i)
16
	}
17
	return accumulator
18
}
19