回滚filter,不然会改变原值

This commit is contained in:
xing 2022-09-24 23:21:09 +08:00
parent acd695709c
commit 6a42a23ef5

View File

@ -191,14 +191,13 @@ func SliceMap[T, R any](arr []T, fn func(T) R) []R {
} }
func SliceFilter[T any](arr []T, fn func(T) bool) []T { func SliceFilter[T any](arr []T, fn func(T) bool) []T {
j := 0 var r []T
for _, t := range arr { for _, t := range arr {
if fn(t) { if fn(t) {
arr[j] = t r = append(r, t)
j++
} }
} }
return arr[:j] return r
} }
func SliceReduce[T, R any](arr []T, fn func(T, R) R, r R) R { func SliceReduce[T, R any](arr []T, fn func(T, R) R, r R) R {