forked from sharpvik/fungi
-
Notifications
You must be signed in to change notification settings - Fork 1
/
zip.go
36 lines (33 loc) · 911 Bytes
/
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
package fungi
// Zip two streams using a custom zipper function. Just like similar operations
// in other languages, zipping will stop on error from either one of the source
// streams. Therefore, if one of the source streams is shorter than the other,
// the resulting stream is going to contain as many elements as the shorter
// source stream.
func Zip[T, R, O any](zipper func(T, R) O) func(Stream[T]) StreamTransformer[R, O] {
return func(itemsT Stream[T]) StreamTransformer[R, O] {
return func(itemsR Stream[R]) Stream[O] {
return &zip[T, R, O]{
ts: itemsT,
rs: itemsR,
zipper: zipper,
}
}
}
}
type zip[T, R, O any] struct {
ts Stream[T]
rs Stream[R]
zipper func(T, R) O
}
func (z *zip[T, R, O]) Next() (item O, err error) {
t, err := z.ts.Next()
if err != nil {
return
}
r, err := z.rs.Next()
if err != nil {
return
}
return z.zipper(t, r), nil
}