underscore/reduce.go
2021-12-29 10:33:00 -04:00

10 lines
322 B
Go

package underscore
// Reduce combine a list of values into a single value.
// acc is the initial state, and each successive step of it should be returned by the reduction function.
func Reduce[T, P any](values []T, reduction func(T, P) P, acc P) P {
for _, v := range values {
acc = reduction(v, acc)
}
return acc
}