From 4e422b4243d22defbe59192b10d8af5b44c8b08c Mon Sep 17 00:00:00 2001 From: Timon Ringwald Date: Sun, 21 Aug 2022 21:20:47 +0200 Subject: [PATCH] flat functions --- flat.go | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 flat.go diff --git a/flat.go b/flat.go new file mode 100644 index 0000000..a070da9 --- /dev/null +++ b/flat.go @@ -0,0 +1,31 @@ +package channel + +func FlatSlice[T any](source <-chan []T) <-chan T { + out := make(chan T, cap(source)) + + go func() { + defer close(out) + for slice := range source { + for _, v := range slice { + out <- v + } + } + }() + + return out +} + +func FlatChan[T any](source <-chan chan T) <-chan T { + out := make(chan T, cap(source)) + + go func() { + defer close(out) + for ch := range source { + for v := range ch { + out <- v + } + } + }() + + return out +}