forked from thoas/go-funk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
typesafe.go
1143 lines (923 loc) · 25.8 KB
/
typesafe.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
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package funk
import (
"math/rand"
)
// InBools is an alias of ContainsBool, returns true if a bool is present in a iteratee.
func InBools(s []bool, v bool) bool {
return ContainsBool(s, v)
}
// InInts is an alias of ContainsInt, returns true if an int is present in a iteratee.
func InInts(s []int, v int) bool {
return ContainsInt(s, v)
}
// InInt32s is an alias of ContainsInt32, returns true if an int32 is present in a iteratee.
func InInt32s(s []int32, v int32) bool {
return ContainsInt32(s, v)
}
// InInt64s is an alias of ContainsInt64, returns true if an int64 is present in a iteratee.
func InInt64s(s []int64, v int64) bool {
return ContainsInt64(s, v)
}
// InUInts is an alias of ContainsUInt, returns true if an uint is present in a iteratee.
func InUInts(s []uint, v uint) bool {
return ContainsUInt(s, v)
}
// InUInt32s is an alias of ContainsUInt32, returns true if an uint32 is present in a iteratee.
func InUInt32s(s []uint32, v uint32) bool {
return ContainsUInt32(s, v)
}
// InUInt64s is an alias of ContainsUInt64, returns true if an uint64 is present in a iteratee.
func InUInt64s(s []uint64, v uint64) bool {
return ContainsUInt64(s, v)
}
// InStrings is an alias of ContainsString, returns true if a string is present in a iteratee.
func InStrings(s []string, v string) bool {
return ContainsString(s, v)
}
// InFloat32s is an alias of ContainsFloat32, returns true if a float32 is present in a iteratee.
func InFloat32s(s []float32, v float32) bool {
return ContainsFloat32(s, v)
}
// InFloat64s is an alias of ContainsFloat64, returns true if a float64 is present in a iteratee.
func InFloat64s(s []float64, v float64) bool {
return ContainsFloat64(s, v)
}
// FindFloat64 iterates over a collection of float64, returning an array of
// all float64 elements predicate returns truthy for.
func FindFloat64(s []float64, cb func(s float64) bool) (float64, bool) {
for _, i := range s {
result := cb(i)
if result {
return i, true
}
}
return 0.0, false
}
// FindFloat32 iterates over a collection of float32, returning the first
// float32 element predicate returns truthy for.
func FindFloat32(s []float32, cb func(s float32) bool) (float32, bool) {
for _, i := range s {
result := cb(i)
if result {
return i, true
}
}
return 0.0, false
}
// FindInt iterates over a collection of int, returning the first
// int element predicate returns truthy for.
func FindInt(s []int, cb func(s int) bool) (int, bool) {
for _, i := range s {
result := cb(i)
if result {
return i, true
}
}
return 0, false
}
// FindInt32 iterates over a collection of int32, returning the first
// int32 element predicate returns truthy for.
func FindInt32(s []int32, cb func(s int32) bool) (int32, bool) {
for _, i := range s {
result := cb(i)
if result {
return i, true
}
}
return 0, false
}
// FindInt64 iterates over a collection of int64, returning the first
// int64 element predicate returns truthy for.
func FindInt64(s []int64, cb func(s int64) bool) (int64, bool) {
for _, i := range s {
result := cb(i)
if result {
return i, true
}
}
return 0, false
}
// FindString iterates over a collection of string, returning the first
// string element predicate returns truthy for.
func FindString(s []string, cb func(s string) bool) (string, bool) {
for _, i := range s {
result := cb(i)
if result {
return i, true
}
}
return "", false
}
// FilterBool iterates over a collection of bool, returning an array of
// all bool elements predicate returns truthy for.
func FilterBool(s []bool, cb func(s bool) bool) []bool {
results := []bool{}
for _, i := range s {
result := cb(i)
if result {
results = append(results, i)
}
}
return results
}
// FilterFloat64 iterates over a collection of float64, returning an array of
// all float64 elements predicate returns truthy for.
func FilterFloat64(s []float64, cb func(s float64) bool) []float64 {
results := []float64{}
for _, i := range s {
result := cb(i)
if result {
results = append(results, i)
}
}
return results
}
// FilterFloat32 iterates over a collection of float32, returning an array of
// all float32 elements predicate returns truthy for.
func FilterFloat32(s []float32, cb func(s float32) bool) []float32 {
results := []float32{}
for _, i := range s {
result := cb(i)
if result {
results = append(results, i)
}
}
return results
}
// FilterInt iterates over a collection of int, returning an array of
// all int elements predicate returns truthy for.
func FilterInt(s []int, cb func(s int) bool) []int {
results := []int{}
for _, i := range s {
result := cb(i)
if result {
results = append(results, i)
}
}
return results
}
// FilterInt32 iterates over a collection of int32, returning an array of
// all int32 elements predicate returns truthy for.
func FilterInt32(s []int32, cb func(s int32) bool) []int32 {
results := []int32{}
for _, i := range s {
result := cb(i)
if result {
results = append(results, i)
}
}
return results
}
// FilterInt64 iterates over a collection of int64, returning an array of
// all int64 elements predicate returns truthy for.
func FilterInt64(s []int64, cb func(s int64) bool) []int64 {
results := []int64{}
for _, i := range s {
result := cb(i)
if result {
results = append(results, i)
}
}
return results
}
// FilterUInt iterates over a collection of uint, returning an array of
// all uint elements predicate returns truthy for.
func FilterUInt(s []uint, cb func(s uint) bool) []uint {
results := []uint{}
for _, i := range s {
result := cb(i)
if result {
results = append(results, i)
}
}
return results
}
// FilterUInt32 iterates over a collection of uint32, returning an array of
// all uint32 elements predicate returns truthy for.
func FilterUInt32(s []uint32, cb func(s uint32) bool) []uint32 {
results := []uint32{}
for _, i := range s {
result := cb(i)
if result {
results = append(results, i)
}
}
return results
}
// FilterUInt64 iterates over a collection of uint64, returning an array of
// all uint64 elements predicate returns truthy for.
func FilterUInt64(s []uint64, cb func(s uint64) bool) []uint64 {
results := []uint64{}
for _, i := range s {
result := cb(i)
if result {
results = append(results, i)
}
}
return results
}
// FilterString iterates over a collection of string, returning an array of
// all string elements predicate returns truthy for.
func FilterString(s []string, cb func(s string) bool) []string {
results := []string{}
for _, i := range s {
result := cb(i)
if result {
results = append(results, i)
}
}
return results
}
// ContainsBool returns true if a boolean is present in a iteratee.
func ContainsBool(s []bool, v bool) bool {
for _, vv := range s {
if vv == v {
return true
}
}
return false
}
// ContainsInt returns true if an int is present in a iteratee.
func ContainsInt(s []int, v int) bool {
for _, vv := range s {
if vv == v {
return true
}
}
return false
}
// ContainsInt32 returns true if an int32 is present in a iteratee.
func ContainsInt32(s []int32, v int32) bool {
for _, vv := range s {
if vv == v {
return true
}
}
return false
}
// ContainsInt64 returns true if an int64 is present in a iteratee.
func ContainsInt64(s []int64, v int64) bool {
for _, vv := range s {
if vv == v {
return true
}
}
return false
}
// ContainsUInt returns true if an uint is present in a iteratee.
func ContainsUInt(s []uint, v uint) bool {
for _, vv := range s {
if vv == v {
return true
}
}
return false
}
// ContainsUInt32 returns true if an uint32 is present in a iteratee.
func ContainsUInt32(s []uint32, v uint32) bool {
for _, vv := range s {
if vv == v {
return true
}
}
return false
}
// ContainsUInt64 returns true if an uint64 is present in a iteratee.
func ContainsUInt64(s []uint64, v uint64) bool {
for _, vv := range s {
if vv == v {
return true
}
}
return false
}
// ContainsString returns true if a string is present in a iteratee.
func ContainsString(s []string, v string) bool {
for _, vv := range s {
if vv == v {
return true
}
}
return false
}
// ContainsFloat32 returns true if a float32 is present in a iteratee.
func ContainsFloat32(s []float32, v float32) bool {
for _, vv := range s {
if vv == v {
return true
}
}
return false
}
// ContainsFloat64 returns true if a float64 is present in a iteratee.
func ContainsFloat64(s []float64, v float64) bool {
for _, vv := range s {
if vv == v {
return true
}
}
return false
}
// SumInt32 sums a int32 iteratee and returns the sum of all elements
func SumInt32(s []int32) (sum int32) {
for _, v := range s {
sum += v
}
return
}
// SumInt64 sums a int64 iteratee and returns the sum of all elements
func SumInt64(s []int64) (sum int64) {
for _, v := range s {
sum += v
}
return
}
// SumInt sums a int iteratee and returns the sum of all elements
func SumInt(s []int) (sum int) {
for _, v := range s {
sum += v
}
return
}
// SumUInt32 sums a uint32 iteratee and returns the sum of all elements
func SumUInt32(s []uint32) (sum uint32) {
for _, v := range s {
sum += v
}
return
}
// SumUInt64 sums a uint64 iteratee and returns the sum of all elements
func SumUInt64(s []uint64) (sum uint64) {
for _, v := range s {
sum += v
}
return
}
// SumUInt sums a uint iteratee and returns the sum of all elements
func SumUInt(s []uint) (sum uint) {
for _, v := range s {
sum += v
}
return
}
// SumFloat64 sums a float64 iteratee and returns the sum of all elements
func SumFloat64(s []float64) (sum float64) {
for _, v := range s {
sum += v
}
return
}
// SumFloat32 sums a float32 iteratee and returns the sum of all elements
func SumFloat32(s []float32) (sum float32) {
for _, v := range s {
sum += v
}
return
}
// ReverseBools reverses an array of bool
func ReverseBools(s []bool) []bool {
for i, j := 0, len(s)-1; i < len(s)/2; i, j = i+1, j-1 {
s[i], s[j] = s[j], s[i]
}
return s
}
// ReverseStrings reverses an array of string
func ReverseStrings(s []string) []string {
for i, j := 0, len(s)-1; i < len(s)/2; i, j = i+1, j-1 {
s[i], s[j] = s[j], s[i]
}
return s
}
// ReverseInt reverses an array of int
func ReverseInt(s []int) []int {
for i, j := 0, len(s)-1; i < len(s)/2; i, j = i+1, j-1 {
s[i], s[j] = s[j], s[i]
}
return s
}
// ReverseInt32 reverses an array of int32
func ReverseInt32(s []int32) []int32 {
for i, j := 0, len(s)-1; i < len(s)/2; i, j = i+1, j-1 {
s[i], s[j] = s[j], s[i]
}
return s
}
// ReverseInt64 reverses an array of int64
func ReverseInt64(s []int64) []int64 {
for i, j := 0, len(s)-1; i < len(s)/2; i, j = i+1, j-1 {
s[i], s[j] = s[j], s[i]
}
return s
}
// ReverseUInt reverses an array of int
func ReverseUInt(s []uint) []uint {
for i, j := 0, len(s)-1; i < len(s)/2; i, j = i+1, j-1 {
s[i], s[j] = s[j], s[i]
}
return s
}
// ReverseUInt32 reverses an array of uint32
func ReverseUInt32(s []uint32) []uint32 {
for i, j := 0, len(s)-1; i < len(s)/2; i, j = i+1, j-1 {
s[i], s[j] = s[j], s[i]
}
return s
}
// ReverseUInt64 reverses an array of uint64
func ReverseUInt64(s []uint64) []uint64 {
for i, j := 0, len(s)-1; i < len(s)/2; i, j = i+1, j-1 {
s[i], s[j] = s[j], s[i]
}
return s
}
// ReverseFloat64 reverses an array of float64
func ReverseFloat64(s []float64) []float64 {
for i, j := 0, len(s)-1; i < len(s)/2; i, j = i+1, j-1 {
s[i], s[j] = s[j], s[i]
}
return s
}
// ReverseFloat32 reverses an array of float32
func ReverseFloat32(s []float32) []float32 {
for i, j := 0, len(s)-1; i < len(s)/2; i, j = i+1, j-1 {
s[i], s[j] = s[j], s[i]
}
return s
}
// ReverseString reverses a string
func ReverseString(s string) string {
r := []rune(s)
for i, j := 0, len(r)-1; i < len(r)/2; i, j = i+1, j-1 {
r[i], r[j] = r[j], r[i]
}
return string(r)
}
func indexOf(n int, f func(int) bool) int {
for i := 0; i < n; i++ {
if f(i) {
return i
}
}
return -1
}
// IndexOfBool gets the index at which the first occurrence of a bool value is found in array or return -1
// if the value cannot be found
func IndexOfBool(a []bool, x bool) int {
return indexOf(len(a), func(i int) bool { return a[i] == x })
}
// IndexOfInt gets the index at which the first occurrence of an int value is found in array or return -1
// if the value cannot be found
func IndexOfInt(a []int, x int) int {
return indexOf(len(a), func(i int) bool { return a[i] == x })
}
// IndexOfInt32 gets the index at which the first occurrence of an int32 value is found in array or return -1
// if the value cannot be found
func IndexOfInt32(a []int32, x int32) int {
return indexOf(len(a), func(i int) bool { return a[i] == x })
}
// IndexOfInt64 gets the index at which the first occurrence of an int64 value is found in array or return -1
// if the value cannot be found
func IndexOfInt64(a []int64, x int64) int {
return indexOf(len(a), func(i int) bool { return a[i] == x })
}
// IndexOfUInt gets the index at which the first occurrence of an uint value is found in array or return -1
// if the value cannot be found
func IndexOfUInt(a []uint, x uint) int {
return indexOf(len(a), func(i int) bool { return a[i] == x })
}
// IndexOfUInt32 gets the index at which the first occurrence of an uint32 value is found in array or return -1
// if the value cannot be found
func IndexOfUInt32(a []uint32, x uint32) int {
return indexOf(len(a), func(i int) bool { return a[i] == x })
}
// IndexOfUInt64 gets the index at which the first occurrence of an uint64 value is found in array or return -1
// if the value cannot be found
func IndexOfUInt64(a []uint64, x uint64) int {
return indexOf(len(a), func(i int) bool { return a[i] == x })
}
// IndexOfFloat64 gets the index at which the first occurrence of an float64 value is found in array or return -1
// if the value cannot be found
func IndexOfFloat64(a []float64, x float64) int {
return indexOf(len(a), func(i int) bool { return a[i] == x })
}
// IndexOfString gets the index at which the first occurrence of a string value is found in array or return -1
// if the value cannot be found
func IndexOfString(a []string, x string) int {
return indexOf(len(a), func(i int) bool { return a[i] == x })
}
func lastIndexOf(n int, f func(int) bool) int {
for i := n - 1; i >= 0; i-- {
if f(i) {
return i
}
}
return -1
}
// LastIndexOfBool gets the index at which the first occurrence of a bool value is found in array or return -1
// if the value cannot be found
func LastIndexOfBool(a []bool, x bool) int {
return lastIndexOf(len(a), func(i int) bool { return a[i] == x })
}
// LastIndexOfInt gets the index at which the first occurrence of an int value is found in array or return -1
// if the value cannot be found
func LastIndexOfInt(a []int, x int) int {
return lastIndexOf(len(a), func(i int) bool { return a[i] == x })
}
// LastIndexOfInt32 gets the index at which the first occurrence of an int32 value is found in array or return -1
// if the value cannot be found
func LastIndexOfInt32(a []int32, x int32) int {
return lastIndexOf(len(a), func(i int) bool { return a[i] == x })
}
// LastIndexOfInt64 gets the index at which the first occurrence of an int64 value is found in array or return -1
// if the value cannot be found
func LastIndexOfInt64(a []int64, x int64) int {
return lastIndexOf(len(a), func(i int) bool { return a[i] == x })
}
// LastIndexOfUInt gets the index at which the first occurrence of an uint value is found in array or return -1
// if the value cannot be found
func LastIndexOfUInt(a []uint, x uint) int {
return lastIndexOf(len(a), func(i int) bool { return a[i] == x })
}
// LastIndexOfUInt32 gets the index at which the first occurrence of an uint32 value is found in array or return -1
// if the value cannot be found
func LastIndexOfUInt32(a []uint32, x uint32) int {
return lastIndexOf(len(a), func(i int) bool { return a[i] == x })
}
// LastIndexOfUInt64 gets the index at which the first occurrence of an uint64 value is found in array or return -1
// if the value cannot be found
func LastIndexOfUInt64(a []uint64, x uint64) int {
return lastIndexOf(len(a), func(i int) bool { return a[i] == x })
}
// LastIndexOfFloat64 gets the index at which the first occurrence of an float64 value is found in array or return -1
// if the value cannot be found
func LastIndexOfFloat64(a []float64, x float64) int {
return lastIndexOf(len(a), func(i int) bool { return a[i] == x })
}
// LastIndexOfFloat32 gets the index at which the first occurrence of an float32 value is found in array or return -1
// if the value cannot be found
func LastIndexOfFloat32(a []float32, x float32) int {
return lastIndexOf(len(a), func(i int) bool { return a[i] == x })
}
// LastIndexOfString gets the index at which the first occurrence of a string value is found in array or return -1
// if the value cannot be found
func LastIndexOfString(a []string, x string) int {
return lastIndexOf(len(a), func(i int) bool { return a[i] == x })
}
// UniqBool creates an array of bool with unique values.
func UniqBool(a []bool) []bool {
results := []bool{}
for _, value := range a {
// If results is not empty, there is at most 1 value in it
if len(results) == 0 || results[0] != value {
results = append(results, value)
}
// At most 2 unique values
if len(results) == 2 {
break
}
}
return results
}
// UniqInt32 creates an array of int32 with unique values.
func UniqInt32(a []int32) []int32 {
var (
length = len(a)
seen = make(map[int32]struct{}, length)
results = make([]int32, 0)
)
for i := 0; i < length; i++ {
v := a[i]
if _, ok := seen[v]; ok {
continue
}
seen[v] = struct{}{}
results = append(results, v)
}
return results
}
// UniqInt64 creates an array of int64 with unique values.
func UniqInt64(a []int64) []int64 {
var (
length = len(a)
seen = make(map[int64]struct{}, length)
results = make([]int64, 0)
)
for i := 0; i < length; i++ {
v := a[i]
if _, ok := seen[v]; ok {
continue
}
seen[v] = struct{}{}
results = append(results, v)
}
return results
}
// UniqInt creates an array of int with unique values.
func UniqInt(a []int) []int {
var (
length = len(a)
seen = make(map[int]struct{}, length)
results = make([]int, 0)
)
for i := 0; i < length; i++ {
v := a[i]
if _, ok := seen[v]; ok {
continue
}
seen[v] = struct{}{}
results = append(results, v)
}
return results
}
// UniqUInt32 creates an array of uint32 with unique values.
func UniqUInt32(a []uint32) []uint32 {
var (
length = len(a)
seen = make(map[uint32]struct{}, length)
results = make([]uint32, 0)
)
for i := 0; i < length; i++ {
v := a[i]
if _, ok := seen[v]; ok {
continue
}
seen[v] = struct{}{}
results = append(results, v)
}
return results
}
// UniqUInt64 creates an array of uint64 with unique values.
func UniqUInt64(a []uint64) []uint64 {
var (
length = len(a)
seen = make(map[uint64]struct{}, length)
results = make([]uint64, 0)
)
for i := 0; i < length; i++ {
v := a[i]
if _, ok := seen[v]; ok {
continue
}
seen[v] = struct{}{}
results = append(results, v)
}
return results
}
// UniqUInt creates an array of uint with unique values.
func UniqUInt(a []uint) []uint {
var (
length = len(a)
seen = make(map[uint]struct{}, length)
results = make([]uint, 0)
)
for i := 0; i < length; i++ {
v := a[i]
if _, ok := seen[v]; ok {
continue
}
seen[v] = struct{}{}
results = append(results, v)
}
return results
}
// UniqString creates an array of string with unique values.
func UniqString(a []string) []string {
var (
length = len(a)
seen = make(map[string]struct{}, length)
results = make([]string, 0)
)
for i := 0; i < length; i++ {
v := a[i]
if _, ok := seen[v]; ok {
continue
}
seen[v] = struct{}{}
results = append(results, v)
}
return results
}
// UniqFloat64 creates an array of float64 with unique values.
func UniqFloat64(a []float64) []float64 {
var (
length = len(a)
seen = make(map[float64]struct{}, length)
results = make([]float64, 0)
)
for i := 0; i < length; i++ {
v := a[i]
if _, ok := seen[v]; ok {
continue
}
seen[v] = struct{}{}
results = append(results, v)
}
return results
}
// UniqFloat32 creates an array of float32 with unique values.
func UniqFloat32(a []float32) []float32 {
var (
length = len(a)
seen = make(map[float32]struct{}, length)
results = make([]float32, 0)
)
for i := 0; i < length; i++ {
v := a[i]
if _, ok := seen[v]; ok {
continue
}
seen[v] = struct{}{}
results = append(results, v)
}
return results
}
// ShuffleBool creates an array of bool shuffled values using Fisher–Yates algorithm
func ShuffleBool(a []bool) []bool {
for i := range a {
j := rand.Intn(i + 1)
a[i], a[j] = a[j], a[i]
}
return a
}
// ShuffleInt creates an array of int shuffled values using Fisher–Yates algorithm
func ShuffleInt(a []int) []int {
for i := range a {
j := rand.Intn(i + 1)
a[i], a[j] = a[j], a[i]
}
return a
}
// ShuffleInt32 creates an array of int32 shuffled values using Fisher–Yates algorithm
func ShuffleInt32(a []int32) []int32 {
for i := range a {
j := rand.Intn(i + 1)
a[i], a[j] = a[j], a[i]
}
return a
}
// ShuffleInt64 creates an array of int64 shuffled values using Fisher–Yates algorithm
func ShuffleInt64(a []int64) []int64 {
for i := range a {
j := rand.Intn(i + 1)
a[i], a[j] = a[j], a[i]
}
return a
}
// ShuffleUInt creates an array of int shuffled values using Fisher–Yates algorithm
func ShuffleUInt(a []uint) []uint {
for i := range a {
j := rand.Intn(i + 1)
a[i], a[j] = a[j], a[i]
}
return a
}
// ShuffleUInt32 creates an array of uint32 shuffled values using Fisher–Yates algorithm
func ShuffleUInt32(a []uint32) []uint32 {
for i := range a {
j := rand.Intn(i + 1)
a[i], a[j] = a[j], a[i]
}
return a
}
// ShuffleUInt64 creates an array of uint64 shuffled values using Fisher–Yates algorithm
func ShuffleUInt64(a []uint64) []uint64 {
for i := range a {
j := rand.Intn(i + 1)
a[i], a[j] = a[j], a[i]
}
return a
}
// ShuffleString creates an array of string shuffled values using Fisher–Yates algorithm
func ShuffleString(a []string) []string {
for i := range a {
j := rand.Intn(i + 1)
a[i], a[j] = a[j], a[i]
}
return a
}
// ShuffleFloat32 creates an array of float32 shuffled values using Fisher–Yates algorithm
func ShuffleFloat32(a []float32) []float32 {
for i := range a {
j := rand.Intn(i + 1)
a[i], a[j] = a[j], a[i]
}
return a
}
// ShuffleFloat64 creates an array of float64 shuffled values using Fisher–Yates algorithm
func ShuffleFloat64(a []float64) []float64 {
for i := range a {
j := rand.Intn(i + 1)
a[i], a[j] = a[j], a[i]
}
return a
}
// DropBool creates a slice with `n` bools dropped from the beginning.