This repository has been archived by the owner on Mar 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
ugo.go
876 lines (717 loc) · 19.6 KB
/
ugo.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
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
// The MIT License (MIT)
//
// Copyright (c) 2016 Alexey Derbyshev
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
// Package ugo is a toolbox, inspired by underscore.js
// This package provide some of the underscore most used functions
//
// Usage:
//
//
// package main
//
// import (
// u "github.com/alxrm/ugo"
// )
//
// func main() {
// strArr := u.Seq{ "nineteen", "three", "eleven", "five", "seventy", "six", "seven", "one" }
//
// lengths := u.Map(strArr, func(cur, _, _ u.Object) u.Object {
// return len(cur.(string))
// })
//
// fmt.Println(lengths) // Output: [8 5 6 4 7 3 5 3]
// }
package ugo
import (
sorter "github.com/alxrm/ugo/timsort"
"math"
"math/rand"
"reflect"
"time"
)
// Object is an alias type for interface{}
type Object interface{}
// Seq is an alias type for (generic) interface{} slice e. g. []interface{}
type Seq []interface{}
// Collector is an alias type for function, used in Reduce based methods, which has following args:
//
// * Object memo
//
// * Object current
//
// * int index
//
// * Seq list
//
// * returns Object: memo, modified after some iteration
type Collector func(memo, current, currentKey, src Object) Object
// Callback is an alias type for function, used to get calculated result, which has following args:
//
// * Object current
//
// * int index
//
// * Seq list
//
// * returns Object: modified Seq element
type Callback func(current, currentKey, src Object) Object
// Comparator is an alias type for function, used to compare one value with another one, which has following args:
//
// * Object left
//
// * Object right
//
// * returns int: -1 for less, 0 for equals, 1 for larger
type Comparator func(left, right Object) int
// Predicate is an alias type for function, used to check value for some condition, which has following args:
//
// * Object current
//
// * int index
//
// * Seq list
//
// * returns bool: true if check has been passed
type Predicate func(current, currentKey, src Object) bool
// Action is an alias type for function, used to do some action based on given values, which has following args:
//
// * Object current
//
// * int index
//
// * Seq list
type Action func(current, currentKey, src Object)
const (
toMin int = -1 /** constant value for incrementing */
toMax int = 1 /** constant value for decrementing */
)
const (
less = -1
equal = 0
larger = 1
)
// Chain method is a start point for chaining behaviour
// like that: u.Chain(Seq).Map(...).Filter(...).Reduce(...).Value()
func Chain(target Seq) *ChainWrapper {
if IsEmpty(target) {
target = Seq{}
}
return &ChainWrapper{Mid: target, Res: target}
}
// NewSeq creates a new Seq instance with given size
func NewSeq(size int) Seq {
return make(Seq, size)
}
// Each Calls cb Action on each element
func Each(seq Seq, cb Action) {
if cb == nil {
return
}
for index, val := range seq {
cb(val, index, seq)
}
}
// ForEach is an alias for Each (see #Each)
func ForEach(seq Seq, cb Action) {
Each(seq, cb)
}
// Map creates new slice same size, every element is the result of Callback
func Map(seq Seq, cb Callback) Seq {
if seq == nil {
return Seq{}
}
if cb == nil {
return seq
}
length := len(seq)
result := NewSeq(length)
for index, val := range seq {
result[index] = cb(val, index, seq)
}
return result
}
// Collect is an alias for Map (see #Map)
func Collect(seq Seq, cb Callback) Seq {
return Map(seq, cb)
}
// Filter creates new slice, contains only elements that passed Predicate check
func Filter(seq Seq, cb Predicate) Seq {
if seq == nil {
return Seq{}
}
if cb == nil {
return seq
}
result := NewSeq(0)
for index, val := range seq {
if cb(val, index, seq) {
result = append(result, val)
}
}
return result
}
// Select is an alias for Filter (see #Filter)
func Select(seq Seq, cb Predicate) Seq {
return Filter(seq, cb)
}
// Reject creates new slice, contains only elements that haven't passed Predicate check
func Reject(seq Seq, cb Predicate) Seq {
if seq == nil {
return Seq{}
}
if cb == nil {
return seq
}
return Filter(seq, negate(cb))
}
// Reduce makes single value from all of the slice elements, iterating from left
func Reduce(seq Seq, cb Collector, initial Object) Object {
var memo Object
if IsEmpty(seq) || cb == nil {
return nil
}
length := len(seq) - 1
if initial == nil {
memo = seq[0]
return createReduce(seq, cb, memo, 1, toMax, length-1)
}
memo = initial
return createReduce(seq, cb, memo, 0, toMax, length)
}
// Inject is an alias for Reduce (see #Reduce)
func Inject(seq Seq, cb Collector, initial Object) Object {
return Reduce(seq, cb, initial)
}
// FoldL is an alias for Reduce (see #Reduce)
func FoldL(seq Seq, cb Collector, initial Object) Object {
return Reduce(seq, cb, initial)
}
// ReduceRight makes single value from all of the slice elements, iterating from right
func ReduceRight(seq Seq, cb Collector, initial Object) Object {
var memo Object
if IsEmpty(seq) || cb == nil {
return nil
}
length := len(seq) - 1
if initial == nil {
memo = seq[length]
return createReduce(seq, cb, memo, length-1, toMin, length-1)
}
memo = initial
return createReduce(seq, cb, memo, length, toMin, length)
}
// FoldR is an alias for Reduce (see #ReduceRight)
func FoldR(seq Seq, cb Collector, initial Object) Object {
return ReduceRight(seq, cb, initial)
}
// Min returns min value from slice, calculated in comparator
func Min(seq Seq, cb Comparator) Object {
return createComparingIterator(seq, cb, toMin, len(seq))
}
// Max returns max value from slice, calculated in comparator
func Max(seq Seq, cb Comparator) Object {
return createComparingIterator(seq, cb, toMax, len(seq))
}
// Find returns first found value, passed the predicate check
func Find(seq Seq, cb Predicate) Object {
length := len(seq) - 1
res, _ := createPredicateSearch(seq, cb, 0, toMax, length)
return res
}
// Detect is an alias for Find (see #Find)
func Detect(seq Seq, cb Predicate) Object {
return Find(seq, cb)
}
// FindLast returns last found value, passed the predicate check
func FindLast(seq Seq, cb Predicate) Object {
length := len(seq) - 1
res, _ := createPredicateSearch(seq, cb, length, toMin, length)
return res
}
// FindIndex returns first found index, which value passed the predicate check
func FindIndex(seq Seq, cb Predicate) int {
length := len(seq) - 1
_, index := createPredicateSearch(seq, cb, 0, toMax, length)
return index
}
// FindLastIndex returns last found index, which value passed the predicate check
func FindLastIndex(seq Seq, cb Predicate) int {
length := len(seq) - 1
_, index := createPredicateSearch(seq, cb, length, toMin, length)
return index
}
// Some returns true if at least one element passed the predicate check
func Some(seq Seq, cb Predicate) bool {
return FindIndex(seq, cb) != -1
}
// Any is an alias for Some (see #Some)
func Any(seq Seq, cb Predicate) bool {
return Some(seq, cb)
}
// IndexOf founds index of the first element, which equals to passed one(target)
// NOTE: if slice is sorted, this method can use better search algorithm
func IndexOf(seq Seq, target Object, isSorted bool, cb Comparator) int {
if cb == nil {
return -1
}
if isSorted {
return createBinarySearch(seq, target, cb, len(seq))
}
equalityPredicate := func(cur, _, _ Object) bool { return cb(cur, target) == 0 }
return FindIndex(seq, equalityPredicate)
}
// LastIndexOf founds index of the last element, which equals to passed one(target)
func LastIndexOf(seq Seq, target Object, cb Comparator) int {
if cb == nil {
return -1
}
var equalityPredicate Predicate = func(cur, _, _ Object) bool { return cb(cur, target) == 0 }
return FindLastIndex(seq, equalityPredicate)
}
// Contains returns true if slice contains element, which equals to passed one(target)
// NOTE: if slice is sorted, this method can use better search algorithm
func Contains(seq Seq, target Object, isSorted bool, cb Comparator) bool {
if cb == nil {
return false
}
return IndexOf(seq, target, isSorted, cb) != -1
}
// Includes is an alias for Contains (see #Contains)
func Includes(seq Seq, target Object, isSorted bool, cb Comparator) bool {
return Contains(seq, target, isSorted, cb)
}
// Every returns true if every element in slice have passed the predicate test
func Every(seq Seq, cb Predicate) bool {
if IsEmpty(seq) || cb == nil {
return false
}
for index, val := range seq {
if !cb(val, index, seq) {
return false
}
}
return true
}
// All is an alias for Every (see #Every)
func All(seq Seq, cb Predicate) bool {
return Every(seq, cb)
}
// Uniq returns slice, which contains only unique elements, calculated by Comparator
func Uniq(seq Seq, cb Comparator) Seq {
if seq == nil {
return Seq{}
}
if cb == nil {
return seq
}
result := NewSeq(0)
for _, value := range seq {
if !Contains(result, value, false, cb) {
result = append(result, value)
}
}
return result
}
// Unique is an alias for Uniq (see #Uniq)
func Unique(seq Seq, cb Comparator) Seq {
return Uniq(seq, cb)
}
// Difference returns the values from slice that are not present in the other slice
func Difference(seq, other Seq, cb Comparator) Seq {
if seq == nil {
return Seq{}
}
if cb == nil || other == nil {
return Seq{}
}
result := NewSeq(0)
for _, value := range seq {
if !Contains(other, value, false, cb) {
result = append(result, value)
}
}
return result
}
// Without returns the Slice without all instances of nonGrata value
func Without(seq Seq, nonGrata Object, cb Comparator) Seq {
if seq == nil || cb == nil {
return Seq{}
}
if nonGrata == nil {
return seq
}
result := NewSeq(0)
for _, value := range seq {
if cb(value, nonGrata) != 0 {
result = append(result, value)
}
}
return result
}
// Intersection returns the values that are intersection of two slices
// Each value in the result is present in each of the arrays.
func Intersection(seq, other Seq, cb Comparator) Seq {
if seq == nil {
return Seq{}
}
if cb == nil || other == nil {
return Seq{}
}
result := NewSeq(0)
for _, value := range seq {
if Contains(other, value, false, cb) {
result = append(result, value)
}
}
return Uniq(result, cb)
}
// Union returns the unique values that are union of two slices
// each value in the result appears at least once in one of the passed slices
func Union(seq, other Seq, cb Comparator) Seq {
if seq == nil {
return Seq{}
}
if cb == nil {
return Seq{}
}
result := Concat(seq, other)
return Uniq(result, cb)
}
// SortBy returns sorted slice, uses very powerful timsort* algorithm
// *timsort obtained from: https://github.com/psilva261/timsort
func SortBy(seq Seq, cb Comparator) Seq {
if seq == nil {
return Seq{}
}
if cb == nil {
return seq
}
sorter.Sort(seq, lessThan(cb))
return seq
}
// CountBy returns map, which values are count of certain kind of values,
// and keys are names of this kinds
func CountBy(seq Seq, cb Callback) (result map[string]int) {
result = make(map[string]int, 0)
key := ""
if seq == nil || cb == nil {
return result
}
for index, val := range seq {
key = cb(val, index, seq).(string)
if result[key] == 0 {
result[key] = 1
} else {
result[key] = result[key] + 1
}
}
return result
}
// GroupBy returns map, which keys are results of Callback calculation,
// and the value is the slice of elements, which gave such result
func GroupBy(seq Seq, cb Callback) map[Object]Seq {
var key Object
var length int
result := make(map[Object]Seq, 0)
if seq == nil || cb == nil {
return result
}
for index, val := range seq {
key = cb(val, index, seq)
length = len(result[key])
if length == 0 {
result[key] = Seq{val}
} else {
result[key] = Insert(result[key], val, length)
}
}
return result
}
// Remove takes an element from given position in slice
func Remove(seq Seq, position int) Seq {
if IsEmpty(seq) {
return Seq{}
}
position = fixPosition(position, len(seq)-1)
bef := NewSeq(len(seq[:position]))
aft := NewSeq(len(seq[position+1:]))
copy(bef, seq[:position])
copy(aft, seq[position+1:])
return Concat(bef, aft)
}
// Insert pushes an element into given position in slice
func Insert(seq Seq, target Object, position int) Seq {
if seq == nil {
return Seq{}
}
position = fixPosition(position, len(seq))
seq = append(seq, 0)
copy(seq[position+1:], seq[position:])
seq[position] = target
return seq
}
// Concat adds another slice to the end of given slice
func Concat(seq, next Seq) Seq {
if seq == nil {
return Seq{}
}
if next == nil {
return seq
}
return append(seq, next...)
}
// Shuffle returns shuffled slice
func Shuffle(seq Seq) Seq {
if seq == nil {
return Seq{}
}
return createShuffle(seq)
}
// ShuffledCopy returns shuffled copy of slice
func ShuffledCopy(seq Seq) Seq {
if seq == nil {
return Seq{}
}
length := len(seq)
copied := NewSeq(length)
copy(copied, seq)
return createShuffle(copied)
}
// Reverse returns reversed slice
func Reverse(seq Seq) Seq {
if seq == nil {
return Seq{}
}
length := len(seq)
return createReverse(seq, length)
}
// ReversedCopy returns reversed copy of slice
func ReversedCopy(seq Seq) Seq {
if seq == nil {
return Seq{}
}
length := len(seq)
copied := NewSeq(length)
copy(copied, seq)
return createReverse(copied, length)
}
// EqualsStrict checks whether both of the given slices are strictly equal,
// e. g. they got the same values in the same positions
func EqualsStrict(seqLeft, seqRight Seq, cb Comparator) bool {
if len(seqLeft) != len(seqRight) || cb == nil {
return false
}
for index, value := range seqLeft {
if cb(seqRight[index], value) != 0 {
return false
}
}
return true
}
// EqualsNotStrict checks whether both of the given slices are equal, but not strictly,
// e. g. they the same values, but positions can be different
func EqualsNotStrict(seqLeft, seqRight Seq, cb Comparator) bool {
lengthLeft := len(seqLeft)
lengthRight := len(seqRight)
target := NewSeq(lengthLeft)
foundIndex := -1
if lengthLeft != lengthRight || cb == nil {
return false
}
copy(target, seqRight)
for _, value := range seqLeft {
if foundIndex = IndexOf(target, value, false, cb); foundIndex != -1 {
target = Remove(target, foundIndex)
} else {
return false
}
}
return true
}
/* Utils */
// Random returns the random number in given range
func Random(min, max float64) int {
if min == max {
return 0
}
rand.Seed(time.Now().UnixNano())
min = fixNumber(min)
max = fixNumber(max)
if min > max {
min, max = max, min
}
res := min + math.Floor(rand.Float64()*(max-min+1))
if math.IsNaN(res) {
return 0
}
return int(res)
}
// IsEmpty returns true if given slice has zero length or it's to nil
func IsEmpty(seq Seq) bool {
return seq == nil || len(seq) == 0
}
// IsSlice returns true if given object has type of slice
func IsSlice(target Object) bool {
return reflect.ValueOf(target).Kind() == reflect.Slice
}
// From returns Seq from given object, filling the resulting Seq via reflection
// NOTE: You should possibly avoid using this one
func From(target Object, size int) Seq {
if !IsSlice(target) || size < 0 {
return Seq{}
}
s := reflect.ValueOf(target)
res := make(Seq, size)
for i := 0; i < size; i++ {
res[i] = s.Index(i).Interface()
}
return res
}
/* private methods */
// createComparingIterator returns single value, which conforms some condition (dir)
// it works for O(n/2) and uses in Min/Max methods
func createComparingIterator(seq Seq, cb Comparator, dir, length int) Object {
var lastComputed Object
var innerComputed Object
if IsEmpty(seq) || cb == nil {
return -1
}
if length == 1 {
return seq[0]
}
lastComputed = seq[0]
for left, right := 0, length-1; left <= right; left, right = left+1, right-1 {
if sgn(cb(seq[left], seq[right])) == dir {
innerComputed = seq[left]
} else {
innerComputed = seq[right]
}
if sgn(cb(innerComputed, lastComputed)) == dir {
lastComputed = innerComputed
}
}
return lastComputed
}
// createShuffle returns single value folded to it from given slice
func createReduce(seq Seq, cb Collector, memo Object, startPoint, direction, length int) Object {
result := memo
index := startPoint
for i := 0; i <= length; i++ {
result = cb(result, seq[index], index, seq)
index += direction
}
return result
}
// createShuffle returns slice shuffled by Fisher-Yates algorithm
func createShuffle(seq Seq) Seq {
for i := range seq {
j := Random(0, float64(i))
seq[i], seq[j] = seq[j], seq[i]
}
return seq
}
// createReverse returns the slice, shuffled in O(n/2) operations
func createReverse(seq Seq, length int) Seq {
for left, right := 0, length-1; left < right; left, right = left+1, right-1 {
seq[left], seq[right] = seq[right], seq[left]
}
return seq
}
// createBinarySearch returns the index of the value we want to find,
// it just goes through given slice and invokes the Predicate check
func createPredicateSearch(seq Seq, cb Predicate, startPoint, direction, length int) (res Object, resIndex int) {
res = nil
resIndex = -1
if seq == nil || cb == nil {
return
}
index := startPoint
for i := 0; i <= length; i++ {
if cb(seq[index], index, seq) {
res = seq[index]
resIndex = index
break
}
index += direction
}
return
}
// createBinarySearch returns the index of the value we want to find,
// it uses the Binary Search algorithm to reduce iteration count
// this assumes that we operating with sorted Sequence
func createBinarySearch(sortedSeq Seq, target Object, cb Comparator, length int) int {
resIndex := -1
lo := 0
hi := length
for lo < hi {
mid := (lo + hi) >> 1
if cb(sortedSeq[mid], target) == 0 {
resIndex = mid
break
}
if cb(sortedSeq[mid], target) < 0 {
lo = mid + 1
} else {
hi = mid
}
}
return resIndex
}
// lessThen returns function we can use for sorting or comparing,
// it simply returns true if the left value is less than the right value
func lessThan(cb Comparator) func(l, r interface{}) bool {
return func(l, r interface{}) bool { return cb(l, r) < 0 }
}
// negate returns the given Predicate but with opposite result
func negate(cb Predicate) Predicate {
return func(cur, index, list Object) bool { return !cb(cur, index, list) }
}
// sgn returns the sign of the passed number, which can be, as follows,
// -1, 0, 1 (negative, zero, positive)
func sgn(num int) int {
if num < 0 {
return less
} else if num == 0 {
return equal
}
return larger
}
// fixPosition returns robust index, that is inside the slice bounds
func fixPosition(pos, ableMax int) int {
if pos < 0 {
return 0
} else if pos > ableMax {
return ableMax
}
return pos
}
// fixNumber returns fixed float64 number we able to use in calculations
func fixNumber(num float64) float64 {
if math.IsNaN(num) {
return 0
} else if math.IsInf(num, -1) {
return math.MinInt64
} else if math.IsInf(num, 1) {
return math.MaxInt64
}
return num
}