-
Notifications
You must be signed in to change notification settings - Fork 225
Expand file tree
/
Copy pathzip.go
More file actions
63 lines (53 loc) · 1.7 KB
/
zip.go
File metadata and controls
63 lines (53 loc) · 1.7 KB
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
package linq
import "iter"
// Zip applies a specified function to the corresponding elements of two
// collections, producing a collection of the results.
//
// The method steps through the two input collections, applying function
// resultSelector to corresponding elements of the two collections. The method
// returns a collection of the values that are returned by resultSelector. If
// the input collections do not have the same number of elements, the method
// combines elements until it reaches the end of one of the collections. For
// example, if one collection has three elements and the other one has four, the
// result collection has only three elements.
func (q Query) Zip(q2 Query,
resultSelector func(any, any) any) Query {
return Query{
Iterate: func(yield func(any) bool) {
next1, stop1 := iter.Pull(q.Iterate)
defer stop1()
next2, stop2 := iter.Pull(q2.Iterate)
defer stop2()
for {
item1, ok1 := next1()
item2, ok2 := next2()
if !ok1 || !ok2 {
return
}
result := resultSelector(item1, item2)
if !yield(result) {
return
}
}
},
}
}
// ZipT is the typed version of Zip.
//
// - resultSelectorFn is of type "func(TFirst,TSecond)TResult"
//
// NOTE: Zip has better performance than ZipT.
func (q Query) ZipT(q2 Query,
resultSelectorFn any) Query {
resultSelectorGenericFunc, err := newGenericFunc(
"ZipT", "resultSelectorFn", resultSelectorFn,
simpleParamValidator(newElemTypeSlice(new(genericType), new(genericType)), newElemTypeSlice(new(genericType))),
)
if err != nil {
panic(err)
}
resultSelectorFunc := func(item1 any, item2 any) any {
return resultSelectorGenericFunc.Call(item1, item2)
}
return q.Zip(q2, resultSelectorFunc)
}