-
Notifications
You must be signed in to change notification settings - Fork 225
Expand file tree
/
Copy pathselectmany.go
More file actions
233 lines (200 loc) · 6.46 KB
/
selectmany.go
File metadata and controls
233 lines (200 loc) · 6.46 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
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
package linq
// SelectMany projects each element of a collection to a Query, iterates and
// flattens the resulting collection into one collection.
func (q Query) SelectMany(selector func(any) Query) Query {
return Query{
Iterate: func(yield func(any) bool) {
q.Iterate(func(outerItem any) bool {
keepGoing := true
innerQuery := selector(outerItem)
innerQuery.Iterate(func(innerItem any) bool {
if !yield(innerItem) {
keepGoing = false
return false
}
return true
})
return keepGoing
})
},
}
}
// SelectManyT is the typed version of SelectMany.
//
// - selectorFn is of type "func(TSource)Query"
//
// NOTE: SelectMany has better performance than SelectManyT.
func (q Query) SelectManyT(selectorFn any) Query {
selectManyGenericFunc, err := newGenericFunc(
"SelectManyT", "selectorFn", selectorFn,
simpleParamValidator(newElemTypeSlice(new(genericType)), newElemTypeSlice(new(Query))),
)
if err != nil {
panic(err)
}
selectorFunc := func(inner any) Query {
return selectManyGenericFunc.Call(inner).(Query)
}
return q.SelectMany(selectorFunc)
}
// 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 (q Query) SelectManyIndexed(selector func(index int, outer any) Query) Query {
return Query{
Iterate: func(yield func(any) bool) {
index := 0
q.Iterate(func(outerItem any) bool {
keepGoing := true
innerQuery := selector(index, outerItem)
index++
innerQuery.Iterate(func(innerItem any) bool {
if !yield(innerItem) {
keepGoing = false
return false
}
return true
})
return keepGoing
})
},
}
}
// SelectManyIndexedT is the typed version of SelectManyIndexed.
//
// - selectorFn is of type "func(int,TSource)Query"
//
// NOTE: SelectManyIndexed has better performance than SelectManyIndexedT.
func (q Query) SelectManyIndexedT(selectorFn any) Query {
selectManyIndexedGenericFunc, err := newGenericFunc(
"SelectManyIndexedT", "selectorFn", selectorFn,
simpleParamValidator(newElemTypeSlice(new(int), new(genericType)), newElemTypeSlice(new(Query))),
)
if err != nil {
panic(err)
}
selectorFunc := func(index int, inner any) Query {
return selectManyIndexedGenericFunc.Call(index, inner).(Query)
}
return q.SelectManyIndexed(selectorFunc)
}
// 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 (q Query) SelectManyBy(
selector func(outer any) Query,
resultSelector func(inner, outer any) any,
) Query {
return Query{
Iterate: func(yield func(any) bool) {
q.Iterate(func(outerItem any) bool {
keepGoing := true
innerQuery := selector(outerItem)
innerQuery.Iterate(func(innerItem any) bool {
result := resultSelector(innerItem, outerItem)
if !yield(result) {
keepGoing = false
return false
}
return true
})
return keepGoing
})
},
}
}
// SelectManyByT is the typed version of SelectManyBy.
//
// - selectorFn is of type "func(TSource)Query"
// - resultSelectorFn is of type "func(TSource,TCollection)TResult"
//
// NOTE: SelectManyBy has better performance than SelectManyByT.
func (q Query) SelectManyByT(selectorFn any,
resultSelectorFn any) Query {
selectorGenericFunc, err := newGenericFunc(
"SelectManyByT", "selectorFn", selectorFn,
simpleParamValidator(newElemTypeSlice(new(genericType)), newElemTypeSlice(new(Query))),
)
if err != nil {
panic(err)
}
selectorFunc := func(outer any) Query {
return selectorGenericFunc.Call(outer).(Query)
}
resultSelectorGenericFunc, err := newGenericFunc(
"SelectManyByT", "resultSelectorFn", resultSelectorFn,
simpleParamValidator(newElemTypeSlice(new(genericType), new(genericType)), newElemTypeSlice(new(genericType))),
)
if err != nil {
panic(err)
}
resultSelectorFunc := func(outer any, item any) any {
return resultSelectorGenericFunc.Call(outer, item)
}
return q.SelectManyBy(selectorFunc, resultSelectorFunc)
}
// 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 (q Query) SelectManyByIndexed(
selector func(index int, outer any) Query,
resultSelector func(inner, outer any) any,
) Query {
return Query{
Iterate: func(yield func(any) bool) {
index := 0
q.Iterate(func(outerItem any) bool {
innerQuery := selector(index, outerItem)
index++
keepGoing := true
innerQuery.Iterate(func(innerItem any) bool {
result := resultSelector(innerItem, outerItem)
if !yield(result) {
keepGoing = false
return false
}
return true
})
return keepGoing
})
},
}
}
// SelectManyByIndexedT is the typed version of SelectManyByIndexed.
//
// - selectorFn is of type "func(int,TSource)Query"
// - resultSelectorFn is of type "func(TSource,TCollection)TResult"
//
// NOTE: SelectManyByIndexed has better performance than
// SelectManyByIndexedT.
func (q Query) SelectManyByIndexedT(selectorFn any,
resultSelectorFn any) Query {
selectorGenericFunc, err := newGenericFunc(
"SelectManyByIndexedT", "selectorFn", selectorFn,
simpleParamValidator(newElemTypeSlice(new(int), new(genericType)), newElemTypeSlice(new(Query))),
)
if err != nil {
panic(err)
}
selectorFunc := func(index int, outer any) Query {
return selectorGenericFunc.Call(index, outer).(Query)
}
resultSelectorGenericFunc, err := newGenericFunc(
"SelectManyByIndexedT", "resultSelectorFn", resultSelectorFn,
simpleParamValidator(newElemTypeSlice(new(genericType), new(genericType)), newElemTypeSlice(new(genericType))),
)
if err != nil {
panic(err)
}
resultSelectorFunc := func(outer any, item any) any {
return resultSelectorGenericFunc.Call(outer, item)
}
return q.SelectManyByIndexed(selectorFunc, resultSelectorFunc)
}