Skip to content

Commit

Permalink
feat: slice 包新增 Filter 函数用于过滤切片
Browse files Browse the repository at this point in the history
  • Loading branch information
kercylan98 committed Aug 22, 2023
1 parent d37fbb7 commit ab19bd6
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions utils/slice/slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,3 +333,15 @@ func SubWithCheck[T any](a, b []T, checkHandle func(a, b T) bool) []T {
}
return result
}

// Filter 过滤切片中的元素
// - filterHandle 返回 true 表示需要保留
func Filter[T any](a []T, filterHandle func(a T) bool) []T {
var result []T
for _, a := range a {
if filterHandle(a) {
result = append(result, a)
}
}
return result
}

0 comments on commit ab19bd6

Please sign in to comment.