flat functions
This commit is contained in:
parent
608e828f82
commit
4e422b4243
31
flat.go
Normal file
31
flat.go
Normal 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
|
||||
}
|
Loading…
Reference in New Issue
Block a user