channel/flat.go

47 lines
739 B
Go
Raw Permalink Normal View History

2022-08-21 21:20:47 +02:00
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
}
2024-05-10 20:32:41 +02:00
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
}
2022-08-21 21:22:16 +02:00
func FlatChan[T any](source <-chan <-chan T) <-chan T {
2022-08-21 21:20:47 +02:00
out := make(chan T, cap(source))
go func() {
defer close(out)
for ch := range source {
for v := range ch {
out <- v
}
}
}()
return out
}