feat: add ReduceRight

This commit is contained in:
dudaodong committed 2023-04-04 17:54:54 +08:00
1 parent f198191063
commit c01c9d14b4
5 files changed
+95 -3

No files matched your search

+12
View File
@@ -504,6 +504,18 @@ func ReduceBy[T any, U any](slice []T, initial U, reducer func(index int, item T
return accumulator
}
// ReduceRight is like ReduceBy, but it iterates over elements of slice from right to left.
// Play: todo
func ReduceRight[T any, U any](slice []T, initial U, reducer func(index int, item T, agg U) U) U {
accumulator := initial
for i := len(slice) - 1; i >= 0; i-- {
accumulator = reducer(i, slice[i], 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 {