From acd695709cea53de03ab002dc7c1eb97c378ecf7 Mon Sep 17 00:00:00 2001 From: xing Date: Sat, 24 Sep 2022 23:04:24 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96filter?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- helper/func.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/helper/func.go b/helper/func.go index 2d36e0d..ef6e0a5 100644 --- a/helper/func.go +++ b/helper/func.go @@ -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 { - var r []T + j := 0 for _, t := range arr { 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 {