优化filter
This commit is contained in:
parent
bdfc171dec
commit
acd695709c
|
@ -191,13 +191,14 @@ 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 {
|
||||||
var r []T
|
j := 0
|
||||||
for _, t := range arr {
|
for _, t := range arr {
|
||||||
if fn(t) {
|
if fn(t) {
|
||||||
r = append(r, t)
|
arr[j] = t
|
||||||
|
j++
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return r
|
return arr[:j]
|
||||||
}
|
}
|
||||||
|
|
||||||
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 {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user