1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-12 16:52:29 +08:00

Merge branch 'main' of github.com:duke-git/lancet into main

This commit is contained in:
dudaodong
2022-01-03 20:11:32 +08:00

View File

@@ -154,18 +154,14 @@ func Filter(slice, function interface{}) interface{} {
panic("function param should be of type func(int, " + elemType.String() + ")" + reflect.ValueOf(true).Type().String()) panic("function param should be of type func(int, " + elemType.String() + ")" + reflect.ValueOf(true).Type().String())
} }
var indexes []int res := reflect.MakeSlice(sv.Type(), 0, 0)
for i := 0; i < sv.Len(); i++ { for i := 0; i < sv.Len(); i++ {
flag := fn.Call([]reflect.Value{reflect.ValueOf(i), sv.Index(i)})[0] flag := fn.Call([]reflect.Value{reflect.ValueOf(i), sv.Index(i)})[0]
if flag.Bool() { if flag.Bool() {
indexes = append(indexes, i) res = reflect.Append(res, sv.Index(i))
} }
} }
res := reflect.MakeSlice(sv.Type(), len(indexes), len(indexes))
for i := range indexes {
res.Index(i).Set(sv.Index(indexes[i]))
}
return res.Interface() return res.Interface()
} }