slices/reduce.go

16 lines
273 B
Go
Raw Normal View History

2023-03-09 14:34:44 +01:00
package slices
2025-02-01 19:43:09 +01:00
import "git.tordarus.net/tordarus/gmath"
2023-03-09 14:34:44 +01:00
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
}