diff --git a/utils/functional.go b/utils/functional.go index c8f69fd..f61dae2 100644 --- a/utils/functional.go +++ b/utils/functional.go @@ -16,6 +16,14 @@ func Reduce[S, T any](source []S, initialValue T, f func(T, S) T) T { return acc } +func ReduceIndexed[S, T any](source []S, initialValue T, f func(T, S, int, []S) T) T { + acc := initialValue + for index, elem := range source { + acc = f(acc, elem, index, source[:]) + } + return acc +} + func Filter[T any](source []T, f func(T) bool) []T { var dest []T for _, elem := range source {