From 6fcfff6744ed197aca95188435bb4e83475d37f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E6=B6=9B?= Date: Fri, 19 Aug 2022 23:24:09 +0800 Subject: [PATCH] =?UTF-8?q?feat(utils):=E5=A2=9E=E5=8A=A0=E4=B8=80?= =?UTF-8?q?=E4=B8=AA=E5=B8=A6=E7=B4=A2=E5=BC=95=E7=9A=84Reduce=E5=87=BD?= =?UTF-8?q?=E6=95=B0=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- utils/functional.go | 8 ++++++++ 1 file changed, 8 insertions(+) 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 {