-
Notifications
You must be signed in to change notification settings - Fork 225
/
result.go
674 lines (551 loc) · 15.5 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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
package linq
import (
"math"
"reflect"
)
// All determines whether all elements of a collection satisfy a condition.
func (q Query) All(predicate func(interface{}) bool) bool {
next := q.Iterate()
for item, ok := next(); ok; item, ok = next() {
if !predicate(item) {
return false
}
}
return true
}
// AllT is the typed version of All.
//
// - predicateFn is of type "func(TSource) bool"
//
// NOTE: All has better performance than AllT.
func (q Query) AllT(predicateFn interface{}) bool {
predicateGenericFunc, err := newGenericFunc(
"AllT", "predicateFn", predicateFn,
simpleParamValidator(newElemTypeSlice(new(genericType)), newElemTypeSlice(new(bool))),
)
if err != nil {
panic(err)
}
predicateFunc := func(item interface{}) bool {
return predicateGenericFunc.Call(item).(bool)
}
return q.All(predicateFunc)
}
// Any determines whether any element of a collection exists.
func (q Query) Any() bool {
_, ok := q.Iterate()()
return ok
}
// AnyWith determines whether any element of a collection satisfies a condition.
func (q Query) AnyWith(predicate func(interface{}) bool) bool {
next := q.Iterate()
for item, ok := next(); ok; item, ok = next() {
if predicate(item) {
return true
}
}
return false
}
// AnyWithT is the typed version of AnyWith.
//
// - predicateFn is of type "func(TSource) bool"
//
// NOTE: AnyWith has better performance than AnyWithT.
func (q Query) AnyWithT(predicateFn interface{}) bool {
predicateGenericFunc, err := newGenericFunc(
"AnyWithT", "predicateFn", predicateFn,
simpleParamValidator(newElemTypeSlice(new(genericType)), newElemTypeSlice(new(bool))),
)
if err != nil {
panic(err)
}
predicateFunc := func(item interface{}) bool {
return predicateGenericFunc.Call(item).(bool)
}
return q.AnyWith(predicateFunc)
}
// Average computes the average of a collection of numeric values.
func (q Query) Average() (r float64) {
next := q.Iterate()
item, ok := next()
if !ok {
return math.NaN()
}
n := 1
switch item.(type) {
case int, int8, int16, int32, int64:
conv := getIntConverter(item)
sum := conv(item)
for item, ok = next(); ok; item, ok = next() {
sum += conv(item)
n++
}
r = float64(sum)
case uint, uint8, uint16, uint32, uint64:
conv := getUIntConverter(item)
sum := conv(item)
for item, ok = next(); ok; item, ok = next() {
sum += conv(item)
n++
}
r = float64(sum)
default:
conv := getFloatConverter(item)
r = conv(item)
for item, ok = next(); ok; item, ok = next() {
r += conv(item)
n++
}
}
return r / float64(n)
}
// Contains determines whether a collection contains a specified element.
func (q Query) Contains(value interface{}) 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 (q Query) Count() (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 (q Query) CountWith(predicate func(interface{}) bool) (r int) {
next := q.Iterate()
for item, ok := next(); ok; item, ok = next() {
if predicate(item) {
r++
}
}
return
}
// CountWithT is the typed version of CountWith.
//
// - predicateFn is of type "func(TSource) bool"
//
// NOTE: CountWith has better performance than CountWithT.
func (q Query) CountWithT(predicateFn interface{}) int {
predicateGenericFunc, err := newGenericFunc(
"CountWithT", "predicateFn", predicateFn,
simpleParamValidator(newElemTypeSlice(new(genericType)), newElemTypeSlice(new(bool))),
)
if err != nil {
panic(err)
}
predicateFunc := func(item interface{}) bool {
return predicateGenericFunc.Call(item).(bool)
}
return q.CountWith(predicateFunc)
}
// First returns the first element of a collection.
func (q Query) First() interface{} {
item, _ := q.Iterate()()
return item
}
// FirstWith returns the first element of a collection that satisfies a
// specified condition.
func (q Query) FirstWith(predicate func(interface{}) bool) interface{} {
next := q.Iterate()
for item, ok := next(); ok; item, ok = next() {
if predicate(item) {
return item
}
}
return nil
}
// FirstWithT is the typed version of FirstWith.
//
// - predicateFn is of type "func(TSource) bool"
//
// NOTE: FirstWith has better performance than FirstWithT.
func (q Query) FirstWithT(predicateFn interface{}) interface{} {
predicateGenericFunc, err := newGenericFunc(
"FirstWithT", "predicateFn", predicateFn,
simpleParamValidator(newElemTypeSlice(new(genericType)), newElemTypeSlice(new(bool))),
)
if err != nil {
panic(err)
}
predicateFunc := func(item interface{}) bool {
return predicateGenericFunc.Call(item).(bool)
}
return q.FirstWith(predicateFunc)
}
// ForEach performs the specified action on each element of a collection.
func (q Query) ForEach(action func(interface{})) {
next := q.Iterate()
for item, ok := next(); ok; item, ok = next() {
action(item)
}
}
// ForEachT is the typed version of ForEach.
//
// - actionFn is of type "func(TSource)"
//
// NOTE: ForEach has better performance than ForEachT.
func (q Query) ForEachT(actionFn interface{}) {
actionGenericFunc, err := newGenericFunc(
"ForEachT", "actionFn", actionFn,
simpleParamValidator(newElemTypeSlice(new(genericType)), nil),
)
if err != nil {
panic(err)
}
actionFunc := func(item interface{}) {
actionGenericFunc.Call(item)
}
q.ForEach(actionFunc)
}
// 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 (q Query) ForEachIndexed(action func(int, interface{})) {
next := q.Iterate()
index := 0
for item, ok := next(); ok; item, ok = next() {
action(index, item)
index++
}
}
// ForEachIndexedT is the typed version of ForEachIndexed.
//
// - actionFn is of type "func(int, TSource)"
//
// NOTE: ForEachIndexed has better performance than ForEachIndexedT.
func (q Query) ForEachIndexedT(actionFn interface{}) {
actionGenericFunc, err := newGenericFunc(
"ForEachIndexedT", "actionFn", actionFn,
simpleParamValidator(newElemTypeSlice(new(int), new(genericType)), nil),
)
if err != nil {
panic(err)
}
actionFunc := func(index int, item interface{}) {
actionGenericFunc.Call(index, item)
}
q.ForEachIndexed(actionFunc)
}
// Last returns the last element of a collection.
func (q Query) Last() (r interface{}) {
next := q.Iterate()
for item, ok := next(); ok; item, ok = next() {
r = item
}
return
}
// LastWith returns the last element of a collection that satisfies a specified
// condition.
func (q Query) LastWith(predicate func(interface{}) bool) (r interface{}) {
next := q.Iterate()
for item, ok := next(); ok; item, ok = next() {
if predicate(item) {
r = item
}
}
return
}
// LastWithT is the typed version of LastWith.
//
// - predicateFn is of type "func(TSource) bool"
//
// NOTE: LastWith has better performance than LastWithT.
func (q Query) LastWithT(predicateFn interface{}) interface{} {
predicateGenericFunc, err := newGenericFunc(
"LastWithT", "predicateFn", predicateFn,
simpleParamValidator(newElemTypeSlice(new(genericType)), newElemTypeSlice(new(bool))),
)
if err != nil {
panic(err)
}
predicateFunc := func(item interface{}) bool {
return predicateGenericFunc.Call(item).(bool)
}
return q.LastWith(predicateFunc)
}
// Max returns the maximum value in a collection of values.
func (q Query) Max() (r interface{}) {
next := q.Iterate()
item, ok := next()
if !ok {
return nil
}
compare := getComparer(item)
r = item
for item, ok := next(); ok; item, ok = next() {
if compare(item, r) > 0 {
r = item
}
}
return
}
// Min returns the minimum value in a collection of values.
func (q Query) Min() (r interface{}) {
next := q.Iterate()
item, ok := next()
if !ok {
return nil
}
compare := getComparer(item)
r = item
for item, ok := next(); ok; item, ok = next() {
if compare(item, r) < 0 {
r = item
}
}
return
}
// Results iterates over a collection and returnes slice of interfaces
func (q Query) Results() (r []interface{}) {
next := q.Iterate()
for item, ok := next(); ok; item, ok = next() {
r = append(r, item)
}
return
}
// SequenceEqual determines whether two collections are equal.
func (q Query) SequenceEqual(q2 Query) 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 (q Query) Single() interface{} {
next := q.Iterate()
item, ok := next()
if !ok {
return nil
}
_, ok = next()
if ok {
return nil
}
return item
}
// SingleWith returns the only element of a collection that satisfies a
// specified condition, and nil if more than one such element exists.
func (q Query) SingleWith(predicate func(interface{}) bool) (r interface{}) {
next := q.Iterate()
found := false
for item, ok := next(); ok; item, ok = next() {
if predicate(item) {
if found {
return nil
}
found = true
r = item
}
}
return
}
// SingleWithT is the typed version of SingleWith.
//
// - predicateFn is of type "func(TSource) bool"
//
// NOTE: SingleWith has better performance than SingleWithT.
func (q Query) SingleWithT(predicateFn interface{}) interface{} {
predicateGenericFunc, err := newGenericFunc(
"SingleWithT", "predicateFn", predicateFn,
simpleParamValidator(newElemTypeSlice(new(genericType)), newElemTypeSlice(new(bool))),
)
if err != nil {
panic(err)
}
predicateFunc := func(item interface{}) bool {
return predicateGenericFunc.Call(item).(bool)
}
return q.SingleWith(predicateFunc)
}
// SumInts 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 (q Query) SumInts() (r int64) {
next := q.Iterate()
item, ok := next()
if !ok {
return 0
}
conv := getIntConverter(item)
r = conv(item)
for item, ok = next(); ok; item, ok = next() {
r += conv(item)
}
return
}
// SumUInts computes the sum of a collection of numeric values.
//
// Values can be of any unsigned integer type: uint, uint8, uint16, uint32,
// uint64. The result is uint64. Method returns zero if collection contains no
// elements.
func (q Query) SumUInts() (r uint64) {
next := q.Iterate()
item, ok := next()
if !ok {
return 0
}
conv := getUIntConverter(item)
r = conv(item)
for item, ok = next(); ok; item, ok = next() {
r += conv(item)
}
return
}
// SumFloats computes the sum of a collection of numeric values.
//
// Values can be of any float type: float32 or float64. The result is float64.
// Method returns zero if collection contains no elements.
func (q Query) SumFloats() (r float64) {
next := q.Iterate()
item, ok := next()
if !ok {
return 0
}
conv := getFloatConverter(item)
r = conv(item)
for item, ok = next(); ok; item, ok = next() {
r += conv(item)
}
return
}
// ToChannel iterates over a collection and outputs each element to a channel,
// then closes it.
func (q Query) ToChannel(result chan<- interface{}) {
next := q.Iterate()
for item, ok := next(); ok; item, ok = next() {
result <- item
}
close(result)
}
// ToChannelT is the typed version of ToChannel.
//
// - result is of type "chan TSource"
//
// NOTE: ToChannel has better performance than ToChannelT.
func (q Query) ToChannelT(result interface{}) {
r := reflect.ValueOf(result)
next := q.Iterate()
for item, ok := next(); ok; item, ok = next() {
r.Send(reflect.ValueOf(item))
}
r.Close()
}
// 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 (q Query) ToMap(result interface{}) {
q.ToMapBy(
result,
func(i interface{}) interface{} {
return i.(KeyValue).Key
},
func(i interface{}) interface{} {
return i.(KeyValue).Value
})
}
// 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 (q Query) ToMapBy(result interface{},
keySelector func(interface{}) interface{},
valueSelector func(interface{}) interface{}) {
res := reflect.ValueOf(result)
m := reflect.Indirect(res)
next := q.Iterate()
for item, ok := next(); ok; item, ok = next() {
key := reflect.ValueOf(keySelector(item))
value := reflect.ValueOf(valueSelector(item))
m.SetMapIndex(key, value)
}
res.Elem().Set(m)
}
// ToMapByT is the typed version of ToMapBy.
//
// - keySelectorFn is of type "func(TSource)TKey"
// - valueSelectorFn is of type "func(TSource)TValue"
//
// NOTE: ToMapBy has better performance than ToMapByT.
func (q Query) ToMapByT(result interface{},
keySelectorFn interface{}, valueSelectorFn interface{}) {
keySelectorGenericFunc, err := newGenericFunc(
"ToMapByT", "keySelectorFn", keySelectorFn,
simpleParamValidator(newElemTypeSlice(new(genericType)), newElemTypeSlice(new(genericType))),
)
if err != nil {
panic(err)
}
keySelectorFunc := func(item interface{}) interface{} {
return keySelectorGenericFunc.Call(item)
}
valueSelectorGenericFunc, err := newGenericFunc(
"ToMapByT", "valueSelectorFn", valueSelectorFn,
simpleParamValidator(newElemTypeSlice(new(genericType)), newElemTypeSlice(new(genericType))),
)
if err != nil {
panic(err)
}
valueSelectorFunc := func(item interface{}) interface{} {
return valueSelectorGenericFunc.Call(item)
}
q.ToMapBy(result, keySelectorFunc, valueSelectorFunc)
}
// 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 (q Query) ToSlice(v interface{}) {
res := reflect.ValueOf(v)
slice := reflect.Indirect(res)
cap := slice.Cap()
res.Elem().Set(slice.Slice(0, cap)) // make len(slice)==cap(slice) from now on
next := q.Iterate()
index := 0
for item, ok := next(); ok; item, ok = next() {
if index >= cap {
slice, cap = grow(slice)
}
slice.Index(index).Set(reflect.ValueOf(item))
index++
}
// reslice the len(res)==cap(res) actual res size
res.Elem().Set(slice.Slice(0, index))
}
// grow grows the slice s by doubling its capacity, then it returns the new
// slice (resliced to its full capacity) and the new capacity.
func grow(s reflect.Value) (v reflect.Value, newCap int) {
cap := s.Cap()
if cap == 0 {
cap = 1
} else {
cap *= 2
}
newSlice := reflect.MakeSlice(s.Type(), cap, cap)
reflect.Copy(newSlice, s)
return newSlice, cap
}