Add compact slice function
This commit is contained in:
parent
9a7f26995f
commit
7722a73ba2
1 changed files with 8 additions and 0 deletions
|
@ -80,3 +80,11 @@ func Contains[T comparable](a []T, b T) bool {
|
||||||
}
|
}
|
||||||
return false
|
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
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue