added OfMap

This commit is contained in:
Timon Ringwald 2022-04-25 20:04:14 +02:00
parent 892814aa5c
commit 944ab39e28
1 changed files with 16 additions and 0 deletions

16
of.go
View File

@ -51,3 +51,19 @@ func OfFunc[T any](ctx context.Context, buffer int, f func() T) <-chan T {
return out
}
// OfMap returns a channel containing the return values of the unmapper function
// applied to any key-value pair in m
// The order is random
func OfMap[K comparable, V, T any](m map[K]V, unmapper func(K, V) T) <-chan T {
out := make(chan T, len(m))
defer func() {
defer close(out)
for k, v := range m {
out <- unmapper(k, v)
}
}()
return out
}