slices/reduce.go
2023-03-09 14:34:44 +01:00

16 lines
268 B
Go

package slices
import "git.milar.in/milarin/gmath"
func Reduce[T, R any](slice []T, reducer func(current R, v T) R) R {
res := new(R)
Each(slice, func(v T) {
*res = reducer(*res, v)
})
return *res
}
func SumReducer[N gmath.Number](a, b N) N {
return a + b
}