-
Notifications
You must be signed in to change notification settings - Fork 22
/
fp.go
2006 lines (1690 loc) · 46.4 KB
/
fp.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 fpgo
import (
"fmt"
"reflect"
"regexp"
"sort"
"sync"
)
/**
Special thanks
* fp functions(Dedupe/Difference/Distinct/IsDistinct/DropEq/Drop/DropLast/DropWhile/IsEqual/IsEqualMap/Every/Exists/Intersection/Keys/Values/Max/Min/MinMax/Merge/IsNeg/IsPos/PMap/Range/Reverse/Minus/Some/IsSubset/IsSuperset/Take/TakeLast/Union/IsZero/Zip/GroupBy/UniqBy/Flatten/Prepend/Partition/Tail/Head/SplitEvery)
* Credit: https://github.com/logic-building/functional-go
* Credit: https://github.com/achannarasappa/pneumatic
**/
type fnObj func(interface{}) interface{}
// Transformer Define Transformer Pattern interface
type Transformer[T any, R any] interface {
TransformedBy() TransformerFunctor[T, R]
}
// TransformerFunctor Functor of Transform
type TransformerFunctor[T any, R any] func(T) R
// ReducerFunctor Functor for Reduce
type ReducerFunctor[T any, R any] func(R, T) R
// Predicate Predicate Functor
type Predicate[T any] func(T) bool
// PredicateErr Predicate Functor
type PredicateErr[T any] func(T, int) (bool, error)
// Comparator Comparator Functor
type Comparator[T any] func(T, T) bool
// Comparable Comparable interface able to be compared
type Comparable[T any] interface {
CompareTo(T) int
}
// Numeric Define Numeric types for Generics
type Numeric interface {
int | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | float32 | float64
}
// Ordered Define Ordered types for Generics
type Ordered interface {
int | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | uintptr | string | float32 | float64
}
// CompareToOrdered A general Compare function for Ordered
func CompareToOrdered[T Ordered](a, b T) int {
if b > a {
return 1
} else if b < a {
return -1
}
return 0
}
// PMapOption Options for PMap usages
type PMapOption struct {
FixedPool int // number of goroutines
RandomOrder bool
}
// Compose Compose the functions from right to left (Math: f(g(x)) Compose: Compose(f, g)(x))
func Compose[T any](fnList ...func(...T) []T) func(...T) []T {
return func(s ...T) []T {
f := fnList[0]
nextFnList := fnList[1:]
if len(fnList) == 1 {
return f(s...)
}
return f(Compose(nextFnList...)(s...)...)
}
}
// ComposeInterface Compose the functions from right to left (Math: f(g(x)) Compose: Compose(f, g)(x))
func ComposeInterface(fnList ...func(...interface{}) []interface{}) func(...interface{}) []interface{} {
return Compose(fnList...)
}
// Pipe Pipe the functions from left to right
func Pipe[T any](fnList ...func(...T) []T) func(...T) []T {
return func(s ...T) []T {
lastIndex := len(fnList) - 1
f := fnList[lastIndex]
nextFnList := fnList[:lastIndex]
if len(fnList) == 1 {
return f(s...)
}
return f(Pipe(nextFnList...)(s...)...)
}
}
// PipeInterface Pipe the functions from left to right
func PipeInterface(fnList ...func(...interface{}) []interface{}) func(...interface{}) []interface{} {
return Pipe(fnList...)
}
// Map Map the values to the function from left to right
func Map[T any, R any](fn TransformerFunctor[T, R], values ...T) []R {
result := make([]R, len(values))
for i, val := range values {
result[i] = fn(val)
}
return result
}
// MapIndexed Map the values to the function from left to right
func MapIndexed[T any, R any](fn func(T, int) R, values ...T) []R {
result := make([]R, len(values))
for i, val := range values {
result[i] = fn(val, i)
}
return result
}
// Reduce Reduce the values from left to right(func(memo,val), starting value, slice)
func Reduce[T any, R any](fn ReducerFunctor[T, R], memo R, input ...T) R {
for i := 0; i < len(input); i++ {
memo = fn(memo, input[i])
}
return memo
}
// ReduceIndexed Reduce the values from left to right(func(memo,val,index), starting value, slice)
func ReduceIndexed[T any, R any](fn func(R, T, int) R, memo R, input ...T) R {
for i := 0; i < len(input); i++ {
memo = fn(memo, input[i], i)
}
return memo
}
// Filter Filter the values by the given predicate function (predicate func, slice)
func Filter[T any](fn func(T, int) bool, input ...T) []T {
list := make([]T, len(input))
newLen := 0
for i := range input {
if fn(input[i], i) {
newLen++
list[newLen-1] = input[i]
}
}
result := list[:newLen]
return result
}
// Reject Reject the values by the given predicate function (predicate func, slice)
func Reject[T any](fn func(T, int) bool, input ...T) []T {
return Filter(func(val T, i int) bool {
return !fn(val, i)
}, input...)
}
// Concat Concat slices
func Concat[T any](mine []T, slices ...[]T) []T {
mineLen := len(mine)
totalLen := mineLen
for _, slice := range slices {
if slice == nil {
continue
}
targetLen := len(slice)
totalLen += targetLen
}
newOne := make([]T, totalLen)
for i, item := range mine {
newOne[i] = item
}
totalIndex := mineLen
for _, slice := range slices {
if slice == nil {
continue
}
target := slice
targetLen := len(target)
for j, item := range target {
newOne[totalIndex+j] = item
}
totalIndex += targetLen
}
return newOne
}
// SortSlice Sort items by Comparator
func SortSlice[T any](fn Comparator[T], input ...T) []T {
Sort(fn, input)
return input
}
// SortOrderedAscending Sort items by Comparator
func SortOrderedAscending[T Ordered](input ...T) []T {
SortOrdered(true, input...)
return input
}
// SortOrderedDescending Sort items by Comparator
func SortOrderedDescending[T Ordered](input ...T) []T {
SortOrdered(false, input...)
return input
}
// SortOrdered Sort items by Comparator
func SortOrdered[T Ordered](ascending bool, input ...T) []T {
if ascending {
Sort(func(a, b T) bool {
return CompareToOrdered(a, b) > 0
}, input)
} else {
Sort(func(a, b T) bool {
return CompareToOrdered(a, b) < 0
}, input)
}
return input
}
// Sort Sort items by Comparator
func Sort[T any](fn Comparator[T], input []T) {
sort.SliceStable(input, func(previous int, next int) bool {
return fn(input[previous], input[next])
})
}
// Dedupe Returns a new list removing consecutive duplicates in list.
func Dedupe[T comparable](list ...T) []T {
var newList []T
lenList := len(list)
for i := 0; i < lenList; i++ {
if i+1 < lenList && list[i] == list[i+1] {
continue
}
newList = append(newList, list[i])
}
return newList
}
// Difference returns a set that is the first set without elements of the remaining sets
// repeated value within list parameter will be ignored
func Difference[T comparable](arrList ...[]T) []T {
if arrList == nil {
return make([]T, 0)
}
resultMap := make(map[T]interface{})
if len(arrList) == 1 {
return Distinct(arrList[0]...)
}
var newList []T
// 1st loop iterates items in 1st array
// 2nd loop iterates all the rest of the arrays
// 3rd loop iterates items in the rest of the arrays
for i := 0; i < len(arrList[0]); i++ {
matchCount := 0
for j := 1; j < len(arrList); j++ {
for _, v := range arrList[j] {
// compare every items in 1st array to every items in the rest of the arrays
if arrList[0][i] == v {
matchCount++
break
}
}
}
if matchCount == 0 {
_, ok := resultMap[arrList[0][i]]
if !ok {
newList = append(newList, arrList[0][i])
resultMap[arrList[0][i]] = true
}
}
}
return newList
}
// Distinct removes duplicates.
//
// Example
// list := []int{8, 2, 8, 0, 2, 0}
// Distinct(list...) // returns [8, 2, 0]
func Distinct[T comparable](list ...T) []T {
// Keep order
resultIndex := 0
maxLen := len(list)
result := make([]T, maxLen)
if maxLen > 0 {
s := make(map[T]bool)
for _, v := range list {
if !s[v] {
result[resultIndex] = v
s[v] = true
resultIndex++
}
}
return result[:resultIndex]
}
return result
}
// DistinctForInterface removes duplicates.
//
// Example
// list := []interface{}{8, 2, 8, 0, 2, 0}
// DistinctForInterface(list...) // returns [8, 2, 0]
func DistinctForInterface(list ...interface{}) []interface{} {
// Keep order
resultIndex := 0
maxLen := len(list)
result := make([]interface{}, maxLen)
if maxLen > 0 {
s := make(map[interface{}]bool)
for _, v := range list {
if !s[v] {
result[resultIndex] = v
s[v] = true
resultIndex++
}
}
return result[:resultIndex]
}
return result
}
// DistinctRandom removes duplicates.(RandomOrder)
func DistinctRandom[T comparable](list ...T) []T {
s := SliceToMap(true, list...)
return Keys(s)
}
// IsDistinct returns true if no two of the arguments are =
func IsDistinct[T comparable](list ...T) bool {
if len(list) == 0 {
return false
}
s := make(map[T]bool)
for _, v := range list {
if _, ok := s[v]; ok {
return false
}
s[v] = true
}
return true
}
// DropEq returns a new list after dropping the given item
//
// Example:
// DropEq(1, 1, 2, 3, 1) // returns [2, 3]
func DropEq[T comparable](num T, list ...T) []T {
var newList []T
for _, v := range list {
if v != num {
newList = append(newList, v)
}
}
return newList
}
// Drop drops N item(s) from the list and returns new list.
// Returns empty list if there is only one item in the list or list empty
func Drop[T any](count int, list ...T) []T {
if count <= 0 {
return list
}
if count >= len(list) {
return make([]T, 0)
}
return list[count:]
}
// DropLast drops last N item(s) from the list and returns new list.
// Returns empty list if there is only one item in the list or list empty
func DropLast[T any](count int, list ...T) []T {
listLen := len(list)
if listLen == 0 || count >= listLen {
return make([]T, 0)
}
return list[:(listLen - count)]
}
// DropWhile drops the items from the list as long as condition satisfies.
//
// Takes two inputs
// 1. Function: takes one input and returns boolean
// 2. list
//
// Returns:
// New List.
// Empty list if either one of arguments or both of them are nil
//
// Example: Drops even number. Returns the remaining items once odd number is found in the list.
// DropWhile(isEven, 4, 2, 3, 4, 5) // Returns [3, 4, 5]
//
// func isEven(num int) bool {
// return num%2 == 0
// }
func DropWhile[T any](f Predicate[T], list ...T) []T {
if f == nil {
return make([]T, 0)
}
var newList []T
for i, v := range list {
if !f(v) {
listLen := len(list)
newList = make([]T, listLen-i)
j := 0
for i < listLen {
newList[j] = list[i]
i++
j++
}
return newList
}
}
return newList
}
// IsEqual Returns true if both list are equal else returns false
func IsEqual[T comparable](list1, list2 []T) bool {
len1 := len(list1)
len2 := len(list2)
if len1 == 0 || len2 == 0 || len1 != len2 {
return false
}
for i := 0; i < len1; i++ {
if list1[i] != list2[i] {
return false
}
}
return true
}
// IsEqualMap Returns true if both maps are equal else returns false
func IsEqualMap[T comparable, R comparable](map1, map2 map[T]R) bool {
len1 := len(map1)
len2 := len(map2)
if len1 == 0 || len2 == 0 || len1 != len2 {
return false
}
for k1, v1 := range map1 {
found := false
for k2, v2 := range map2 {
if k1 == k2 && v1 == v2 {
found = true
break
}
}
if !found {
return false
}
}
return true
}
// // IsEven Returns true if n is even
// func IsEven[T Numeric](v T) bool {
// return v%2 == 0
// }
//
// // IsOdd Returns true if n is odd
// func IsOdd[T Numeric](v T) bool {
// return v%2 != 0
// }
// Every returns true if supplied function returns logical true for every item in the list
//
// Example:
// Every(even, 8, 2, 10, 4) // Returns true
//
// func isEven(num int) bool {
// return num%2 == 0
// }
//
// Every(even) // Returns false
// Every(nil) // Returns false
func Every[T any](f Predicate[T], list ...T) bool {
if f == nil || len(list) == 0 {
return false
}
for _, v := range list {
if !f(v) {
return false
}
}
return true
}
// Exists checks if given item exists in the list
//
// Example:
// Exists(8, 8, 2, 10, 4) // Returns true
// Exists(8) // Returns false
func Exists[T comparable](input T, list ...T) bool {
for _, v := range list {
if v == input {
return true
}
}
return false
}
// ExistsForInterface checks if given item exists in the list
//
// Example:
// ExistsForInterface(8, 8, 2, 10, 4) // Returns true
// ExistsForInterface(8) // Returns false
func ExistsForInterface(input interface{}, list ...interface{}) bool {
for _, v := range list {
if v == input {
return true
}
}
return false
}
// Intersection return a set that is the intersection of the input sets
// repeated value within list parameter will be ignored
func Intersection[T comparable](inputList ...[]T) []T {
inputLen := len(inputList)
if inputList == nil {
return make([]T, 0)
}
if inputLen == 1 {
resultMap := make(map[T]interface{}, len(inputList[0]))
var newList []T
for i := 0; i < len(inputList[0]); i++ {
_, ok := resultMap[inputList[0][i]]
if !ok {
newList = append(newList, inputList[0][i])
resultMap[inputList[0][i]] = true
}
}
return newList
}
resultMap := make(map[T]interface{})
var newList []T
// 1st loop iterates items in 1st array
// 2nd loop iterates all the rest of the arrays
// 3rd loop iterates items in the rest of the arrays
for i := 0; i < len(inputList[0]); i++ {
matchCount := 0
for j := 1; j < inputLen; j++ {
for _, v := range inputList[j] {
// compare every items in 1st array to every items in the rest of the arrays
if inputList[0][i] == v {
matchCount++
break
}
}
}
if matchCount == inputLen-1 {
_, ok := resultMap[inputList[0][i]]
if !ok {
newList = append(newList, inputList[0][i])
resultMap[inputList[0][i]] = true
}
}
}
return newList
}
// IntersectionForInterface return a set that is the intersection of the input sets
// repeated value within list parameter will be ignored
func IntersectionForInterface(inputList ...[]interface{}) []interface{} {
inputLen := len(inputList)
if inputList == nil {
return make([]interface{}, 0)
}
if inputLen == 1 {
resultMap := make(map[interface{}]interface{}, len(inputList[0]))
var newList []interface{}
for i := 0; i < len(inputList[0]); i++ {
_, ok := resultMap[inputList[0][i]]
if !ok {
newList = append(newList, inputList[0][i])
resultMap[inputList[0][i]] = true
}
}
return newList
}
resultMap := make(map[interface{}]interface{})
var newList []interface{}
// 1st loop iterates items in 1st array
// 2nd loop iterates all the rest of the arrays
// 3rd loop iterates items in the rest of the arrays
for i := 0; i < len(inputList[0]); i++ {
matchCount := 0
for j := 1; j < inputLen; j++ {
for _, v := range inputList[j] {
// compare every items in 1st array to every items in the rest of the arrays
if inputList[0][i] == v {
matchCount++
break
}
}
}
if matchCount == inputLen-1 {
_, ok := resultMap[inputList[0][i]]
if !ok {
newList = append(newList, inputList[0][i])
resultMap[inputList[0][i]] = true
}
}
}
return newList
}
// IntersectionMapByKey return a set that is the intersection of the input sets
func IntersectionMapByKey[T comparable, R any](inputList ...map[T]R) map[T]R {
inputLen := len(inputList)
if inputLen == 0 {
return make(map[T]R)
}
if inputLen == 1 {
resultMap := make(map[T]R, len(inputList[0]))
for k, v := range inputList[0] {
resultMap[k] = v
}
return resultMap
}
resultMap := make(map[T]R)
countMap := make(map[T]int)
for _, mapItem := range inputList {
for k, v := range mapItem {
_, exists := resultMap[k]
if !exists {
resultMap[k] = v
}
countMap[k]++
}
}
for k, v := range countMap {
if v < inputLen {
delete(resultMap, k)
}
}
return resultMap
}
// IntersectionMapByKeyForInterface return a set that is the intersection of the input sets
func IntersectionMapByKeyForInterface[R any](inputList ...map[interface{}]R) map[interface{}]R {
inputLen := len(inputList)
if inputLen == 0 {
return make(map[interface{}]R)
}
if inputLen == 1 {
resultMap := make(map[interface{}]R, len(inputList[0]))
for k, v := range inputList[0] {
resultMap[k] = v
}
return resultMap
}
resultMap := make(map[interface{}]R)
countMap := make(map[interface{}]int)
for _, mapItem := range inputList {
for k, v := range mapItem {
_, exists := resultMap[k]
if !exists {
resultMap[k] = v
}
countMap[k]++
}
}
for k, v := range countMap {
if v < inputLen {
delete(resultMap, k)
}
}
return resultMap
}
// Minus all of set1 but not in set2
func Minus[T comparable](set1, set2 []T) []T {
resultIndex := 0
maxLen := len(set1)
result := make([]T, maxLen)
set2Map := SliceToMap(true, set2...)
for _, item := range set1 {
_, exists := set2Map[item]
if !exists {
result[resultIndex] = item
resultIndex++
}
}
return result[:resultIndex]
}
// MinusForInterface all of set1 but not in set2
func MinusForInterface(set1, set2 []interface{}) []interface{} {
resultIndex := 0
maxLen := len(set1)
result := make([]interface{}, maxLen)
set2Map := SliceToMapForInterface(true, set2...)
for _, item := range set1 {
_, exists := set2Map[item]
if !exists {
result[resultIndex] = item
resultIndex++
}
}
return result[:resultIndex]
}
// MinusMapByKey all of set1 but not in set2
func MinusMapByKey[T comparable, R any](set1, set2 map[T]R) map[T]R {
resultMap := make(map[T]R, len(set1))
for k, v := range set1 {
_, exists := set2[k]
if !exists {
resultMap[k] = v
}
}
return resultMap
}
// Keys returns a slice of map's keys
func Keys[T comparable, R any](m map[T]R) []T {
keys := make([]T, len(m))
i := 0
for k := range m {
keys[i] = k
i++
}
return keys
}
// KeysForInterface returns a slice of map's keys
func KeysForInterface[R any](m map[interface{}]R) []interface{} {
keys := make([]interface{}, len(m))
i := 0
for k := range m {
keys[i] = k
i++
}
return keys
}
// Values returns a slice of map's values
func Values[T comparable, R any](m map[T]R) []R {
keys := make([]R, len(m))
i := 0
for _, v := range m {
keys[i] = v
i++
}
return keys
}
// ValuesForInterface returns a slice of map's values
func ValuesForInterface[R any](m map[interface{}]R) []R {
keys := make([]R, len(m))
i := 0
for _, v := range m {
keys[i] = v
i++
}
return keys
}
// Max returns max item from the list.
// Return 0 if the list is either empty or nil
func Max[T Numeric](list ...T) T {
if list == nil || len(list) == 0 {
return 0
}
result := list[0]
for _, v := range list {
if v > result {
result = v
}
}
return result
}
// Min returns min item from the list.
// Return 0 if the list is either empty or nil
func Min[T Numeric](list ...T) T {
if list == nil || len(list) == 0 {
return 0
}
result := list[0]
for _, v := range list {
if v < result {
result = v
}
}
return result
}
// MinMax returns min and max items from the list.
// Return 0,0 if the list is either empty or nil
func MinMax[T Numeric](list ...T) (T, T) {
if list == nil || len(list) == 0 {
return 0, 0
}
min := list[0]
max := list[0]
for _, v := range list {
if v < min {
min = v
} else if v > max {
max = v
}
}
return min, max
}
// Merge takes two inputs: map[T]R and map[T]R and merge two maps and returns a new map[T]R.
func Merge[T comparable, R any](map1, map2 map[T]R) map[T]R {
if map1 == nil && map2 == nil {
return map[T]R{}
}
newMap := make(map[T]R, len(map1)+len(map2))
if map1 == nil {
for k, v := range map2 {
newMap[k] = v
}
return newMap
}
if map2 == nil {
for k, v := range map1 {
newMap[k] = v
}
return newMap
}
for k, v := range map1 {
newMap[k] = v
}
for k, v := range map2 {
newMap[k] = v
}
return newMap
}
// MergeForInterface takes two inputs: map[T]R and map[T]R and merge two maps and returns a new map[T]R.
func MergeForInterface[R any](map1, map2 map[interface{}]R) map[interface{}]R {
if map1 == nil && map2 == nil {
return map[interface{}]R{}
}
newMap := make(map[interface{}]R, len(map1)+len(map2))
if map1 == nil {
for k, v := range map2 {
newMap[k] = v
}
return newMap
}
if map2 == nil {
for k, v := range map1 {
newMap[k] = v
}
return newMap
}
for k, v := range map1 {
newMap[k] = v
}
for k, v := range map2 {
newMap[k] = v
}
return newMap
}
// IsNeg Returns true if num is less than zero, else false
func IsNeg[T Numeric](v T) bool {
if v < 0 {
return true
}
return false
}
// IsPos Returns true if num is great than zero, else false
func IsPos[T Numeric](v T) bool {
if v > 0 {
return true
}
return false
}
// PMap applies the function(1st argument) on each item in the list and returns a new list.
// Order of new list is guaranteed. This feature can be disabled by passing: PMapOption{RandomOrder: true} to gain performance
// Run in parallel. no_of_goroutines = no_of_items_in_list or 3rd argument can be passed to fix the number of goroutines.
//
// Takes 3 inputs. 3rd argument is option
// 1. Function - takes 1 input
// 2. optional argument - PMapOption{FixedPool: <some_number>}
// 3. List
func PMap[T any, R any](f TransformerFunctor[T, R], option *PMapOption, list ...T) []R {
if f == nil {
return make([]R, 0)
}
worker := len(list)
if option != nil {
if option.FixedPool > 0 && option.FixedPool < worker {
worker = option.FixedPool
}
if option.RandomOrder == true {
return pMapNoOrder(f, list, worker)
}
}
return pMapPreserveOrder(f, list, worker)
}
func pMapPreserveOrder[T any, R any](f TransformerFunctor[T, R], list []T, worker int) []R {
chJobs := make(chan map[int]T, len(list))
go func() {
for i, v := range list {
chJobs <- map[int]T{i: v}
}
close(chJobs)
}()
chResult := make(chan map[int]R, worker/3)
var wg sync.WaitGroup
for i := 0; i < worker; i++ {
wg.Add(1)
go func(chResult chan map[int]R, chJobs chan map[int]T) {