-
Notifications
You must be signed in to change notification settings - Fork 2
/
flatten_chan.go
41 lines (35 loc) · 990 Bytes
/
flatten_chan.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package pipe
import (
"fmt"
"reflect"
)
// FlattenChan is of type: func(input chan []T) chan T.
// Takes a chan of arrays, and concatenates them together, putting each element
// onto the output chan. After input is closed, output is also closed. If input
// is chan T instead of type chan []T, then this is a no-op.
func FlattenChan(input interface{}) interface{} {
inputValue := reflect.ValueOf(input)
if inputValue.Kind() != reflect.Chan {
panic(fmt.Sprintf("FlattenChan called on invalid type: %s", inputValue.Type()))
}
elemType := inputValue.Type().Elem()
if elemType.Kind() != reflect.Array &&
elemType.Kind() != reflect.Slice {
return input
}
outputType := reflect.ChanOf(reflect.BothDir, elemType.Elem())
output := reflect.MakeChan(outputType, 0)
go func() {
for {
value, ok := inputValue.Recv()
if !ok {
break
}
for i := 0; i < value.Len(); i++ {
output.Send(value.Index(i))
}
}
output.Close()
}()
return output.Interface()
}