From 7722a73ba268333b579513411cb4a12e0b4ebb73 Mon Sep 17 00:00:00 2001 From: mstar Date: Sun, 1 Dec 2024 10:29:28 +0100 Subject: [PATCH] Add compact slice function --- sliceutils/sliceUtils.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/sliceutils/sliceUtils.go b/sliceutils/sliceUtils.go index c9cc0fb..00d6703 100644 --- a/sliceutils/sliceUtils.go +++ b/sliceutils/sliceUtils.go @@ -80,3 +80,11 @@ func Contains[T comparable](a []T, b T) bool { } return false } + +func Compact[T any](a []T, compactor func(acc T, next T) T) T { + var acc T + for _, v := range a { + acc = compactor(acc, v) + } + return acc +}