From 78436e629cc4780b9ffaea20741095209fb33bd0 Mon Sep 17 00:00:00 2001 From: milarin Date: Fri, 10 May 2024 20:32:41 +0200 Subject: [PATCH] FlatMap function introduced --- flat.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/flat.go b/flat.go index 9d112f8..5ce1f25 100644 --- a/flat.go +++ b/flat.go @@ -15,6 +15,21 @@ func FlatSlice[T any](source <-chan []T) <-chan T { return out } +func FlatMap[K comparable, V, T any](source <-chan map[K]V, unmapper func(key K, value V) T) <-chan T { + out := make(chan T, cap(source)) + + go func() { + defer close(out) + for slice := range source { + for k, v := range slice { + out <- unmapper(k, v) + } + } + }() + + return out +} + func FlatChan[T any](source <-chan <-chan T) <-chan T { out := make(chan T, cap(source))