underscore/maps/map.go
Ruidy f73905ddb0
feat: maps map (#9)
Co-authored-by: Ruidy <rnemausat@newstore.com>
2022-01-27 16:15:33 +01:00

16 lines
369 B
Go

package maps
type M[K comparable, V any] map[K]V
// Map produces a new slice of values by mapping each value in the slice through
// a transform function.
func Map[K, Q comparable, V, W any](m M[K, V], f func(K, V) M[Q, W]) M[Q, W] {
res := make(M[Q, W], len(m))
for k, v := range m {
mm := f(k, v)
for k2, v2 := range mm {
res[k2] = v2
}
}
return res
}