feat(utils):增加一个带索引的Reduce函数。
This commit is contained in:
parent
b426c6a7b1
commit
6fcfff6744
|
@ -16,6 +16,14 @@ func Reduce[S, T any](source []S, initialValue T, f func(T, S) T) T {
|
||||||
return acc
|
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 {
|
func Filter[T any](source []T, f func(T) bool) []T {
|
||||||
var dest []T
|
var dest []T
|
||||||
for _, elem := range source {
|
for _, elem := range source {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user