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

feat: add Filter func

This commit is contained in:
dudaodong
2022-03-30 19:52:43 +08:00
parent 3ad142d5d7
commit cbb46f9cb4
2 changed files with 34 additions and 0 deletions

View File

@@ -45,3 +45,15 @@ func ForEach[K comparable, V any](m map[K]V, iteratee func(key K, value V)) {
iteratee(k, v)
}
}
// Filter iterates over map, return a new map contains all key and value pairs pass the predicate function
func Filter[K comparable, V any](m map[K]V, predicate func(key K, value V) bool) map[K]V {
res := make(map[K]V)
for k, v := range m {
if predicate(k, v) {
res[k] = v
}
}
return res
}