-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathresult.go
414 lines (326 loc) · 8.98 KB
/
result.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
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
package flinx
import (
"math"
"golang.org/x/exp/constraints"
_ "golang.org/x/exp/constraints"
)
// All determines whether all elements of a collection satisfy a condition.
func All[T any](predicates ...func(T) bool) func(q Query[T]) bool {
predicate := Predicates(predicates...)
return func(q Query[T]) bool {
next := q.Iterate()
for item, ok := next(); ok; item, ok = next() {
if !predicate(item) {
return false
}
}
return true
}
}
// Any determines whether any element of a collection exists.
func Any[T any](q Query[T]) bool {
_, ok := q.Iterate()()
return ok
}
// AnyWith determines whether any element of a collection satisfies a condition.
func AnyWith[T any](predicates ...func(T) bool) func(q Query[T]) bool {
predicate := Predicates(predicates...)
return func(q Query[T]) bool {
next := q.Iterate()
for item, ok := next(); ok; item, ok = next() {
if predicate(item) {
return true
}
}
return false
}
}
// Average computes the average of a collection of numeric values.
func Average[T constraints.Integer | constraints.Float](q Query[T]) (r float64) {
next := q.Iterate()
item, ok := next()
if !ok {
return math.NaN()
}
sum := item
n := 1
for item, ok = next(); ok; item, ok = next() {
sum += item
n++
}
r = float64(sum)
return r / float64(n)
}
// Contains determines whether a collection contains a specified element.
func Contains[T comparable](value T) func(q Query[T]) bool {
return func(q Query[T]) bool {
next := q.Iterate()
for item, ok := next(); ok; item, ok = next() {
if item == value {
return true
}
}
return false
}
}
// Count returns the number of elements in a collection.
func Count[T any](q Query[T]) (r int) {
next := q.Iterate()
for _, ok := next(); ok; _, ok = next() {
r++
}
return
}
// CountWith returns a number that represents how many elements in the specified
// collection satisfy a condition.
func CountWith[T any](predicates ...func(T) bool) func(q Query[T]) (r int) {
predicate := Predicates(predicates...)
return func(q Query[T]) (r int) {
next := q.Iterate()
for item, ok := next(); ok; item, ok = next() {
if predicate(item) {
r++
}
}
return
}
}
// First returns the first element of a collection.
func First[T any](q Query[T]) (T, bool) {
return q.Iterate()()
}
// FirstWith returns the first element of a collection that satisfies a
// specified condition.
func FirstWith[T any](predicates ...func(T) bool) func(q Query[T]) (T, bool) {
predicate := Predicates(predicates...)
return func(q Query[T]) (T, bool) {
next := q.Iterate()
for item, ok := next(); ok; item, ok = next() {
if predicate(item) {
return item, ok
}
}
var r T
return r, false
}
}
// ForEach performs the specified action on each element of a collection.
func ForEach[T any](action func(T)) func(q Query[T]) {
return func(q Query[T]) {
next := q.Iterate()
for item, ok := next(); ok; item, ok = next() {
action(item)
}
}
}
// ForEachIndexed performs the specified action on each element of a collection.
//
// The first argument to action 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 action represents the
// element to process.
func ForEachIndexed[T any](action func(int, T)) func(q Query[T]) {
return func(q Query[T]) {
next := q.Iterate()
index := 0
for item, ok := next(); ok; item, ok = next() {
action(index, item)
index++
}
}
}
// Last returns the last element of a collection.
func Last[T any](q Query[T]) (r T, exist bool) {
next := q.Iterate()
for item, ok := next(); ok; item, ok = next() {
r = item
exist = true
}
return
}
// LastWith returns the last element of a collection that satisfies a specified
// condition.
func LastWith[T any](predicates ...func(T) bool) func(q Query[T]) (r T, exist bool) {
predicate := Predicates(predicates...)
return func(q Query[T]) (r T, exist bool) {
next := q.Iterate()
for item, ok := next(); ok; item, ok = next() {
if predicate(item) {
r = item
exist = true
}
}
return
}
}
// Max returns the maximum value in a collection of values.
func Max[T any](compare func(t1, t2 T) int) func(q Query[T]) (r T, exist bool) {
return func(q Query[T]) (r T, exist bool) {
next := q.Iterate()
item, ok := next()
if !ok {
return
}
r = item
exist = true
for item, ok := next(); ok; item, ok = next() {
if compare(item, r) > 0 {
r = item
exist = true
}
}
return
}
}
// Min returns the minimum value in a collection of values.
func Min[T any](compare func(t1, t2 T) int) func(q Query[T]) (r T, exist bool) {
return func(q Query[T]) (r T, exist bool) {
next := q.Iterate()
item, ok := next()
if !ok {
return
}
r = item
exist = true
for item, ok := next(); ok; item, ok = next() {
if compare(item, r) < 0 {
r = item
exist = true
}
}
return
}
}
// Results iterates over a collection and returnes slice of interfaces
func Results[T any](q Query[T]) (r []T) {
next := q.Iterate()
for item, ok := next(); ok; item, ok = next() {
r = append(r, item)
}
return
}
// SequenceEqual determines whether two collections are equal.
func SequenceEqual[T comparable](q, q2 Query[T]) bool {
next := q.Iterate()
next2 := q2.Iterate()
for item, ok := next(); ok; item, ok = next() {
item2, ok2 := next2()
if !ok2 || item != item2 {
return false
}
}
_, ok2 := next2()
return !ok2
}
// Single returns the only element of a collection, and nil if there is not
// exactly one element in the collection.
func Single[T any](q Query[T]) (r T, found bool) {
next := q.Iterate()
item, ok := next()
if !ok {
return
}
_, ok = next()
if ok {
return
}
return item, true
}
// SingleWith returns the only element of a collection that satisfies a
// specified condition, and nil if more than one such element exists.
func SingleWith[T any](predicates ...func(T) bool) func(q Query[T]) (r T, found bool) {
predicate := Predicates(predicates...)
return func(q Query[T]) (r T, found bool) {
next := q.Iterate()
for item, ok := next(); ok; item, ok = next() {
if predicate(item) {
if found {
var v T
return v, false
}
found = true
r = item
}
}
return
}
}
// Sum computes the sum of a collection of numeric values.
//
// Values can be of any integer type: int, int8, int16, int32, int64. The result
// is int64. Method returns zero if collection contains no elements.
func Sum[T constraints.Integer | constraints.Float](q Query[T]) (r T) {
next := q.Iterate()
item, ok := next()
if !ok {
return 0
}
r = item
for item, ok = next(); ok; item, ok = next() {
r += item
}
return
}
// ToChannel iterates over a collection and outputs each element to a channel,
// then closes it.
func ToChannel[T any](q Query[T], result chan<- T) {
defer close(result)
next := q.Iterate()
for item, ok := next(); ok; item, ok = next() {
result <- item
}
}
// ToMap iterates over a collection and populates result map with elements.
// Collection elements have to be of KeyValue type to use this method. To
// populate a map with elements of different type use ToMapBy method. ToMap
// doesn't empty the result map before populating it.
func ToMap[K comparable, V any](q Query[KeyValue[K, V]]) map[K]V {
m := map[K]V{}
next := q.Iterate()
for item, ok := next(); ok; item, ok = next() {
m[item.Key] = item.Value
}
return m
}
func ToMapFromGroup[K comparable, V any](q Query[Group[K, V]]) map[K][]V {
m := map[K][]V{}
next := q.Iterate()
for item, ok := next(); ok; item, ok = next() {
m[item.Key] = append(m[item.Key], item.Group...)
}
return m
}
// ToMapBy iterates over a collection and populates the result map with
// elements. Functions keySelector and valueSelector are executed for each
// element of the collection to generate key and value for the map. Generated
// key and value types must be assignable to the map's key and value types.
// ToMapBy doesn't empty the result map before populating it.
func ToMapBy[K comparable, V, T any](keySelector func(T) K, valueSelector func(T) V) func(q Query[T]) map[K]V {
return func(q Query[T]) map[K]V {
m := map[K]V{}
next := q.Iterate()
for item, ok := next(); ok; item, ok = next() {
m[keySelector(item)] = valueSelector(item)
}
return m
}
}
// ToSlice iterates over a collection and saves the results in the slice pointed
// by v. It overwrites the existing slice, starting from index 0.
//
// If the slice pointed by v has sufficient capacity, v will be pointed to a
// resliced slice. If it does not, a new underlying array will be allocated and
// v will point to it.
func ToSlice[T any](q Query[T]) []T {
r := []T{}
next := q.Iterate()
for item, ok := next(); ok; item, ok = next() {
r = append(r, item)
}
return r
}
func ToString(q Query[rune]) string {
return string(ToSlice[rune](q))
}