added Reduce method
This commit is contained in:
15
reduce.go
Normal file
15
reduce.go
Normal file
@ -0,0 +1,15 @@
|
||||
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
|
||||
}
|
Reference in New Issue
Block a user