1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-13 17:22:27 +08:00

feat: add ReduceBy

This commit is contained in:
dudaodong
2023-04-04 17:41:37 +08:00
parent 8bdd46bda4
commit f198191063
5 changed files with 122 additions and 0 deletions

View File

@@ -492,6 +492,18 @@ func Reduce[T any](slice []T, iteratee func(index int, item1, item2 T) T, initia
return result
}
// ReduceBy produces a value from slice by accumulating the result of each element as passed through the reducer function.
// Play: todo
func ReduceBy[T any, U any](slice []T, initial U, reducer func(index int, item T, agg U) U) U {
accumulator := initial
for i, v := range slice {
accumulator = reducer(i, v, accumulator)
}
return accumulator
}
// Replace returns a copy of the slice with the first n non-overlapping instances of old replaced by new.
// Play: https://go.dev/play/p/P5mZp7IhOFo
func Replace[T comparable](slice []T, old T, new T, n int) []T {