feat(utils):增加一个带索引的Reduce函数。

This commit is contained in:
徐涛 2022-08-19 23:24:09 +08:00
parent b426c6a7b1
commit 6fcfff6744

View File

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