-
Notifications
You must be signed in to change notification settings - Fork 3
/
zip.go
64 lines (59 loc) · 1.47 KB
/
zip.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package fp
import (
"fmt"
"reflect"
)
func (q *stream) Zip(other Stream, fn interface{}) Stream {
if isNilStream(other) {
return other
}
fnTyp := reflect.TypeOf(fn)
fnVal := reflect.ValueOf(fn)
onext := other.ToSource().Next
return newStream(newCtx(q.ctx), fnTyp.Out(0), q.iter, func(next iterator) iterator {
return func() (reflect.Value, bool) {
if val1, ok1 := next(); ok1 {
if val2, ok2 := onext(); ok2 {
return fnVal.Call([]reflect.Value{val1, val2})[0], true
}
}
return reflect.Value{}, false
}
})
}
func (q *stream) ZipN(fn interface{}, others ...Stream) Stream {
for _, s := range others {
if isNilStream(s) {
return s
}
}
fnTyp := reflect.TypeOf(fn)
fnVal := reflect.ValueOf(fn)
if fnTyp.NumIn() != len(others)+1 {
panic(fmt.Sprintf("zip function must have %v input param", len(others)+1))
}
return newStream(newCtx(q.ctx), fnTyp.Out(0), q.iter, func(next iterator) iterator {
/* build iterator list */
var iteratorList []iterator
StreamOf(others).Map(func(s Stream) iterator {
return s.ToSource().Next
}).Prepend(next).
ToSlice(&iteratorList)
var done bool
return func() (reflect.Value, bool) {
if !done {
input := make([]reflect.Value, len(others)+1)
for i := range iteratorList {
if val, ok := iteratorList[i](); ok {
input[i] = val
} else {
done = true
return reflect.Value{}, false
}
}
return fnVal.Call(input)[0], true
}
return reflect.Value{}, false
}
})
}