flat functions

This commit is contained in:
Timon Ringwald 2022-08-21 21:20:47 +02:00
parent 608e828f82
commit 4e422b4243

31
flat.go Normal file
View File

@ -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
}