-
Notifications
You must be signed in to change notification settings - Fork 2
/
flatten_slice.go
36 lines (30 loc) · 994 Bytes
/
flatten_slice.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
package pipe
import (
"fmt"
"reflect"
)
// FlattenSlice is of type: func(input [][]T) []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 []T instead of type [][]T, then this is a no-op.
func FlattenSlice(input interface{}) interface{} {
inputValue := reflect.ValueOf(input)
if inputValue.Kind() != reflect.Slice &&
inputValue.Kind() != reflect.Array {
panic(fmt.Sprintf("FlattenSlice called on invalid type: %s", inputValue.Type()))
}
elemType := inputValue.Type().Elem()
if elemType.Kind() != reflect.Array &&
elemType.Kind() != reflect.Slice {
return input
}
outputType := reflect.SliceOf(elemType.Elem())
output := reflect.MakeSlice(outputType, 0, 1)
for i := 0; i < inputValue.Len(); i++ {
items := inputValue.Index(i)
for j := 0; j < items.Len(); j++ {
output = reflect.Append(output, items.Index(j))
}
}
return output.Interface()
}