forked from thoas/go-funk
/
typesafe.go
646 lines (518 loc) · 14 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
package funk
import "math/rand"
// 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)
}
// 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
}
// 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
}
// 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
}
// 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
}
// 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
}
// 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
}
// 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
}
// 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
}
// 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 })
}
// 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
}
// 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 })
}
// 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 })
}
// UniqInt32 creates an array of int32 with unique values.
func UniqInt32(a []int32) []int32 {
length := len(a)
seen := make(map[int32]struct{}, length)
j := 0
for i := 0; i < length; i++ {
v := a[i]
if _, ok := seen[v]; ok {
continue
}
seen[v] = struct{}{}
a[j] = v
j++
}
return a[0:j]
}
// UniqInt64 creates an array of int64 with unique values.
func UniqInt64(a []int64) []int64 {
length := len(a)
seen := make(map[int64]struct{}, length)
j := 0
for i := 0; i < length; i++ {
v := a[i]
if _, ok := seen[v]; ok {
continue
}
seen[v] = struct{}{}
a[j] = v
j++
}
return a[0:j]
}
// UniqInt creates an array of int with unique values.
func UniqInt(a []int) []int {
length := len(a)
seen := make(map[int]struct{}, length)
j := 0
for i := 0; i < length; i++ {
v := a[i]
if _, ok := seen[v]; ok {
continue
}
seen[v] = struct{}{}
a[j] = v
j++
}
return a[0:j]
}
// UniqString creates an array of string with unique values.
func UniqString(a []string) []string {
length := len(a)
seen := make(map[string]struct{}, length)
j := 0
for i := 0; i < length; i++ {
v := a[i]
if _, ok := seen[v]; ok {
continue
}
seen[v] = struct{}{}
a[j] = v
j++
}
return a[0:j]
}
// UniqFloat64 creates an array of float64 with unique values.
func UniqFloat64(a []float64) []float64 {
length := len(a)
seen := make(map[float64]struct{}, length)
j := 0
for i := 0; i < length; i++ {
v := a[i]
if _, ok := seen[v]; ok {
continue
}
seen[v] = struct{}{}
a[j] = v
j++
}
return a[0:j]
}
// UniqFloat32 creates an array of float32 with unique values.
func UniqFloat32(a []float32) []float32 {
length := len(a)
seen := make(map[float32]struct{}, length)
j := 0
for i := 0; i < length; i++ {
v := a[i]
if _, ok := seen[v]; ok {
continue
}
seen[v] = struct{}{}
a[j] = v
j++
}
return a[0:j]
}
// 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
}
// 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
}