From 944ab39e2882857c971273a147b289112f31ee4f Mon Sep 17 00:00:00 2001 From: Timon Ringwald Date: Mon, 25 Apr 2022 20:04:14 +0200 Subject: [PATCH] added OfMap --- of.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/of.go b/of.go index d14e687..af33cd1 100644 --- a/of.go +++ b/of.go @@ -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 +}