-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathslice.go
549 lines (468 loc) · 13.2 KB
/
slice.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
package gogu
import (
"errors"
"fmt"
)
// Sum returns the sum of the slice items. These have to satisfy the type constraints declared as Number.
func Sum[T Number](slice []T) T {
var acc T
for _, v := range slice {
acc += v
}
return acc
}
// SumBy is like Sum except it accept a callback function which is invoked
// for each element in the slice to generate the value to be summed.
func SumBy[T1 any, T2 Number](slice []T1, fn func(T1) T2) T2 {
var acc T2
for _, v := range slice {
acc += fn(v)
}
return acc
}
// Mean computes the mean value of the slice elements.
func Mean[T Number](slice []T) T {
var result T
for i := 0; i < len(slice); i++ {
result += slice[i]
}
return result / T(len(slice))
}
// IndexOf returns the index of the firs occurrence of a value
// in the slice, or -1 if value is not present in the slice.
func IndexOf[T comparable](s []T, val T) int {
for k, v := range s {
if v == val {
return k
}
}
return -1
}
// LastIndexOf returns the index of the last occurrence of a value.
func LastIndexOf[T comparable](s []T, val T) int {
for i, j := len(s)-1, 0; i >= 0; i, j = i-1, j+1 {
if s[i] == val {
return i
}
}
return -1
}
// Map produces a new slice of values by mapping each value in the list through a transformation function.
func Map[T1, T2 any](slice []T1, fn func(T1) T2) []T2 {
result := make([]T2, len(slice))
for idx, v := range slice {
result[idx] = fn(v)
}
return result
}
// ForEach iterates over the elements of a collection and invokes the callback fn function on each element.
func ForEach[T any](slice []T, fn func(T)) {
for _, v := range slice {
fn(v)
}
}
// ForEachRight is the same as ForEach, but starts the iteration from the last element.
func ForEachRight[T any](slice []T, fn func(T)) {
for i := len(slice) - 1; i >= 0; i-- {
fn(slice[i])
}
}
// Reduce reduces the collection to a value which is the accumulated result of running
// each element in the collection through the callback function yielding a single value.
func Reduce[T1, T2 any](slice []T1, fn func(T1, T2) T2, initVal T2) T2 {
actual := initVal
for _, v := range slice {
actual = fn(v, actual)
}
return actual
}
// Reverse reverses the order of elements, so that the first element becomes the last,
// the second element becomes the second to last, and so on.
func Reverse[T any](sl []T) []T {
for i, j := 0, len(sl)-1; i < j; i, j = i+1, j-1 {
sl[i], sl[j] = sl[j], sl[i]
}
return sl
}
// Unique returns the collection unique values.
func Unique[T comparable](slice []T) []T {
keys := make(map[T]bool)
result := []T{}
for _, v := range slice {
if _, ok := keys[v]; !ok {
keys[v] = true
result = append(result, v)
}
}
return result
}
// UniqueBy is like Unique except that it accept a callback function which is invoked on each
// element of the slice applying the criteria by which the uniqueness is computed.
func UniqueBy[T comparable](slice []T, fn func(T) T) []T {
keys := make(map[T]bool)
result := []T{}
for _, v := range slice {
if _, ok := keys[fn(v)]; !ok {
keys[fn(v)] = true
result = append(result, v)
}
}
return result
}
// Every returns true if all the elements of a slice satisfies the criteria of the callback function.
func Every[T any](slice []T, fn func(T) bool) bool {
for _, v := range slice {
if !fn(v) {
return false
}
}
return true
}
// Some returns true if some elements of a slice satisfies the criteria of the callback function.
func Some[T any](slice []T, fn func(T) bool) bool {
for _, v := range slice {
if fn(v) {
return true
}
}
return false
}
// Partition splits the collection elements into two, the ones which satisfies the condition
// expressed in the callback function (fn) and those which does not satisfy the condition.
func Partition[T comparable](slice []T, fn func(T) bool) [2][]T {
var result = [2][]T{}
for _, v := range slice {
if fn(v) {
result[0] = append(result[0], v)
} else {
result[1] = append(result[1], v)
}
}
return result
}
// Contains returns true if the value is present in the collection.
func Contains[T comparable](slice []T, value T) bool {
for _, v := range slice {
if v == value {
return true
}
}
return false
}
// Duplicate returns the duplicated values of a collection.
func Duplicate[T comparable](slice []T) []T {
keyCount := make(map[T]int)
result := make([]T, 0, len(slice))
// Count how many times a value is showing up in the provided collection.
for _, v := range slice {
if _, ok := keyCount[v]; !ok {
keyCount[v] = 1
} else {
keyCount[v]++
}
}
// Include only the values which count frequency is greater than 1 into the resulting slice.
for k, v := range keyCount {
if v > 1 {
result = append(result, k)
}
}
return result
}
// DuplicateWithIndex puts the duplicated values of a collection into a map as a key value pair,
// where the key is the collection element and the value is its position.
func DuplicateWithIndex[T comparable](slice []T) map[T]int {
var count int
kvMap := make(map[T][]int)
result := make(map[T]int)
// Count how many times a value is showing up in the provided collection.
for idx, v := range slice {
if _, ok := kvMap[v]; !ok {
// Create a slice with a dimension of 2, which first element contains the position (the index)
// of the first found duplicate value and the second indicates the number of appearance.
kvMap[v] = make([]int, 2)
count = 1
kvMap[v][0] = idx
kvMap[v][1] = count
} else {
count++
kvMap[v][1] = count
}
}
// Include into the resulting slice only the values which frequency is greater than 1.
for k, v := range kvMap {
if v[1] > 1 {
result[k] = v[0]
}
}
return result
}
// Merge merges the first slice with the other slices defined as variadic parameter.
func Merge[T any](s []T, params ...[]T) []T {
merged := make([]T, 0, len(s))
for i := 0; i < len(params); i++ {
merged = append(merged, params[i]...)
}
merged = append(s, merged...)
return merged
}
// Flatten flattens the slice all the way down to the deepest nesting level.
func Flatten[T any](slice any) ([]T, error) {
return baseFlatten([]T{}, slice)
}
func baseFlatten[T any](acc []T, slice any) ([]T, error) {
var err error
switch v := any(slice).(type) {
case T:
acc = append(acc, v)
case []T:
acc = append(acc, v...)
case []any:
for _, sv := range v {
acc, err = baseFlatten(acc, sv)
if err != nil {
return nil, errors.New("flattening error")
}
}
default:
return nil, errors.New("flattening error")
}
return acc, nil
}
// Union computes the union of the passed\-in slice and returns an
// ordered list of unique items that are present in one or more of the slices.
func Union[T comparable](slice any) ([]T, error) {
var err error
if flatten, err := baseFlatten([]T{}, slice); err == nil {
return Unique(flatten), nil
}
return nil, err
}
// Intersection computes the list of values that are the intersection of all the slices.
// Each value in the result should be present in each of the provided slices.
func Intersection[T comparable](params ...[]T) []T {
result := []T{}
for i := 0; i < len(params[0]); i++ {
item := params[0][i]
if Contains(result, item) {
continue
}
var j int
for j = 1; j < len(params); j++ {
if !Contains(params[j], item) {
break
}
}
if j == len(params) {
result = append(result, item)
}
}
return result
}
// IntersectionBy is like Intersection, except that it accepts and callback function which is invoked on each element of the collection.
func IntersectionBy[T comparable](fn func(T) T, params ...[]T) []T {
result := []T{}
for i := 0; i < len(params[0]); i++ {
item := params[0][i]
if Contains(result, fn(item)) {
continue
}
var j int
for j = 1; j < len(params); j++ {
has := func() bool {
for _, v := range params[j] {
if fn(v) == fn(item) {
return true
}
}
return false
}
if !has() {
break
}
}
if j == len(params) {
result = append(result, item)
}
}
return result
}
// Without returns a copy of the slice with all the values defined in the variadic parameter removed.
func Without[T1 comparable, T2 any](slice []T1, values ...T1) []T1 {
keys := make(map[T1]bool)
uni := make([]T1, 0, len(slice))
loop:
for _, v := range slice {
for _, val := range values {
if v == val {
continue loop
}
}
if _, ok := keys[v]; !ok {
keys[v] = true
uni = append(uni, v)
}
}
return uni
}
// Difference is similar to Without, but returns the values from
// the first slice that are not present in the second slice.
func Difference[T comparable](s1, s2 []T) []T {
keys := make(map[T]bool)
unique := []T{}
loop:
for _, v := range s1 {
for _, val := range s2 {
if v == val {
continue loop
}
}
if _, ok := keys[v]; !ok {
keys[v] = true
unique = append(unique, v)
}
}
return unique
}
// DifferenceBy is like Difference, except that invokes a callback function on each
// element of the slice, applying the criteria by which the difference is computed.
func DifferenceBy[T comparable](s1, s2 []T, fn func(T) T) []T {
keys := make(map[T]bool)
unique := []T{}
loop:
for _, v := range s1 {
for _, val := range s2 {
if fn(v) == fn(val) {
continue loop
}
}
if _, ok := keys[v]; !ok {
keys[v] = true
unique = append(unique, v)
}
}
return unique
}
// Chunk split the slice into groups of slices each having the length of size.
// In case the source slice cannot be distributed equally, the last slice will contain fewer elements.
func Chunk[T comparable](slice []T, size int) [][]T {
var result = make([][]T, 0, len(slice)/2+1)
if size <= 0 {
panic("Chunk size should be greater than zero.")
}
for i := 0; i < len(slice); i++ {
if i%size == 0 {
if i+size < len(slice) {
result = append(result, slice[i:i+size])
} else {
result = append(result, slice[i:])
}
}
}
return result
}
// Drop creates a new slice with n elements dropped from the beginning.
// If n < 0 the elements will be dropped from the back of the collection.
func Drop[T any](slice []T, n int) []T {
if Abs(n) < len(slice) {
if n > 0 {
return slice[n:]
} else {
return slice[:len(slice)-Abs(n)]
}
}
return []T{}
}
// DropWhile creates a new slice excluding the elements dropped from the beginning.
// Elements are dropped by applying the condition invoked in the callback function.
func DropWhile[T any](slice []T, fn func(T) bool) []T {
result := make([]T, 0, len(slice))
for _, v := range slice {
if !fn(v) {
result = append(result, v)
}
}
return result
}
// DropRightWhile creates a new slice excluding the elements dropped from the end.
// Elements are dropped by applying the condition invoked in the callback function.
func DropRightWhile[T any](slice []T, fn func(T) bool) []T {
result := make([]T, 0, len(slice))
for i := len(slice) - 1; i >= 0; i-- {
if !fn(slice[i]) {
result = append(result, slice[i])
}
}
return result
}
// MapByIndex
func mapByIndex[T1 comparable, T2 any](origSlice []T2, mapSlice []T1) map[T1][]T2 {
result := make(map[T1][]T2)
for idx, v := range mapSlice {
if _, ok := result[v]; !ok {
result[v] = make([]T2, 0, len(mapSlice))
}
result[v] = append(result[v], origSlice[idx])
}
return result
}
// GroupBy splits a collection into a key-value set, grouped by the result of running each value through the callback function fn.
// The return value is a map where the key is the conditional logic of the callback function
// and the values are the callback function returned values.
func GroupBy[T1, T2 comparable](slice []T1, fn func(T1) T2) map[T2][]T1 {
return mapByIndex(slice, Map(slice, fn))
}
// Zip iteratively merges together the values of the slice parameters with the values at the corresponding position.
func Zip[T any](slices ...[]T) [][]T {
var result = make([][]T, len(slices))
var sliceLen int
if len(slices) > 0 {
sliceLen = len(slices[0])
}
if sliceLen != len(slices) {
panic(fmt.Sprintf("the number of slice parameters (%d) does not match with the slice length (%d)", len(slices), sliceLen))
}
for idx, sl := range slices {
if sliceLen != len(sl) {
panic("the slice parameters should have identical length")
}
result[idx] = make([]T, len(sl))
}
for x := 0; x < sliceLen; x++ {
for i := 0; i < len(slices); i++ {
result[i][x] = slices[x][i]
}
}
return result
}
// Unzip is the opposite of Zip: given a slice of slices it returns a series of new slices,
// the first of which contains all the first elements in the input slices,
// the second of which contains all the second elements, and so on.
func Unzip[T any](slices ...[]T) [][]T {
var result = make([][]T, len(slices))
var sliceLen int
if len(slices) > 0 {
sliceLen = len(slices[0])
}
if sliceLen != len(slices) {
panic(fmt.Sprintf("the number of slice parameters (%d) does not match with the slice length (%d)", len(slices), sliceLen))
}
for idx, sl := range slices {
if sliceLen != len(sl) {
panic("the slice parameters should have identical length")
}
result[idx] = make([]T, len(sl))
}
for x := 0; x < sliceLen; x++ {
for i := 0; i < len(slices); i++ {
result[x][i] = slices[i][x]
}
}
return result
}
// ToSlice returns the function arguments as a slice.
func ToSlice[T any](args ...T) []T {
slice := make([]T, 0, len(args))
slice = append(slice, args...)
return slice
}