Skip to content

Commit

Permalink
add Collect() for maps
Browse files Browse the repository at this point in the history
  • Loading branch information
cbarbian-sap committed Feb 1, 2024
1 parent 3fe613b commit 8b6266c
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions maps/maps.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,15 @@ func Equal[K comparable, V comparable](m map[K]V, n map[K]V) bool {
}
return EqualBy(m, n, f)
}

// Collect map through given function.
func Collect[K comparable, V any, W any](m map[K]V, f func(V) W) map[K]W {
if m == nil {
return nil
}
n := make(map[K]W)
for k, v := range m {
n[k] = f(v)
}
return n
}

0 comments on commit 8b6266c

Please sign in to comment.