Add compact slice function

This commit is contained in:
Melody Becker 2024-12-01 10:29:28 +01:00
parent 9a7f26995f
commit 7722a73ba2

View file

@ -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
}