-
Notifications
You must be signed in to change notification settings - Fork 1
/
selectmany.go
181 lines (156 loc) · 4.16 KB
/
selectmany.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package flinx
// SelectMany projects each element of a collection to a Query, iterates and
// flattens the resulting collection into one collection.
func SelectMany[T, V any](selector func(T) Query[V]) func(q Query[T]) Query[V] {
selectorIdx := func(_ int, t T) Query[V] {
return selector(t)
}
return SelectManyIndexed[T, V](selectorIdx)
//return func(q Query[T]) Query[V] {
// return Query[V]{
// Iterate: func() Iterator[V] {
// outernext := q.Iterate()
// var inner *T
// var innernext Iterator[V]
// return func() (item V, ok bool) {
// for !ok {
// if inner == nil {
// inner, ok = func() (*T, bool) {
// itemT, exist := outernext()
// return &itemT, exist
// }()
// if !ok {
// return
// }
//
// innernext = selector(*inner).Iterate()
// }
//
// item, ok = innernext()
// if !ok {
// inner = nil
// }
// }
//
// return
// }
// },
// }
//}
}
// SelectManyIndexed projects each element of a collection to a Query, iterates
// and flattens the resulting collection into one collection.
//
// The first argument to selector represents the zero-based index of that
// element in the source collection. This can be useful if the elements are in a
// known order and you want to do something with an element at a particular
// index, for example. It can also be useful if you want to retrieve the index
// of one or more elements. The second argument to selector represents the
// element to process.
func SelectManyIndexed[T, V any](selector func(int, T) Query[V]) func(q Query[T]) Query[V] {
return func(q Query[T]) Query[V] {
return Query[V]{
Iterate: func() Iterator[V] {
outernext := q.Iterate()
index := 0
var inner *T
var innernext Iterator[V]
return func() (item V, ok bool) {
for !ok {
if inner == nil {
inner, ok = func() (*T, bool) {
itemT, exist := outernext()
return &itemT, exist
}()
if !ok {
return
}
innernext = selector(index, *inner).Iterate()
index++
}
item, ok = innernext()
if !ok {
inner = nil
}
}
return
}
},
}
}
}
// SelectManyBy projects each element of a collection to a Query, iterates and
// flattens the resulting collection into one collection, and invokes a result
// selector function on each element therein.
func SelectManyBy[T, V, O any](selector func(T) Query[V],
resultSelector func(V, T) O) func(q Query[T]) Query[O] {
return func(q Query[T]) Query[O] {
return Query[O]{
Iterate: func() Iterator[O] {
outernext := q.Iterate()
var outer *T
var innernext Iterator[V]
return func() (item O, ok bool) {
var v V
for !ok {
if outer == nil {
outer, ok = func() (*T, bool) {
outerTmp, exist := outernext()
return &outerTmp, exist
}()
if !ok {
return
}
innernext = selector(*outer).Iterate()
}
v, ok = innernext()
if !ok {
outer = nil
}
}
item = resultSelector(v, *outer)
return
}
},
}
}
}
// SelectManyByIndexed projects each element of a collection to a Query,
// iterates and flattens the resulting collection into one collection, and
// invokes a result selector function on each element therein. The index of each
// source element is used in the intermediate projected form of that element.
func SelectManyByIndexed[T, V, O any](selector func(int, T) Query[V],
resultSelector func(V, T) O) func(q Query[T]) Query[O] {
return func(q Query[T]) Query[O] {
return Query[O]{
Iterate: func() Iterator[O] {
outernext := q.Iterate()
index := 0
var outer *T
var innernext Iterator[V]
return func() (item O, ok bool) {
var v V
for !ok {
if outer == nil {
outer, ok = func() (*T, bool) {
outerTmp, exist := outernext()
return &outerTmp, exist
}()
if !ok {
return
}
innernext = selector(index, *outer).Iterate()
index++
}
v, ok = innernext()
if !ok {
outer = nil
}
}
item = resultSelector(v, *outer)
return
}
},
}
}
}