-
Notifications
You must be signed in to change notification settings - Fork 2
/
slice.go2
479 lines (423 loc) · 10.3 KB
/
slice.go2
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
// Functions that operate on slices
//
// Usage Notes
//
// • arity - all functions have a arity of two wherein the function is prepared with all arguments in the first call and the input data is applied in the second call
package slice
import (
"sort"
)
// Map applies an iterator function to each element in a slice and returns a slice with the new elements
//
// Parameters
//
// • iterator: function that's applied to each element
// • data: slice of elements
//
// Return Value
//
// • result: slice of elements with the iterator applied
//
func Map[TValue any, TResult any](iterator func(TValue) TResult) func([]TValue) []TResult {
return func(data []TValue) []TResult {
out := make([]TResult, len(data))
for i, v := range data {
out[i] = iterator(v)
}
return out
}
}
// MapIndexed is the same as Map but also makes the index and input elements available to the iterator function
//
// Parameters
//
// • iterator: function that's applied to each element
// • data: list of elements
//
// Return Value
//
// • result: list of elements with the iterator applied
//
func MapIndexed[TValue any, TResult any](iterator func(TValue, int, []TValue) TResult) func([]TValue) []TResult {
return func(data []TValue) []TResult {
out := make([]TResult, len(data))
for i, v := range data {
out[i] = iterator(v, i, data)
}
return out
}
}
// Reduce iterates over a slice and calls the iterator function on each element while passing the accumulator to the next call
//
// Parameters
//
// • iterator: function that's applied to each element
// • initialAcc: initial value of the accumulator
// • data: list of elements
//
// Return Value
//
// • result: the list of elements reduced to a single data type
//
func Reduce[TValue any, TAcc any](iterator func(TAcc, TValue, int) TAcc, initialAcc TAcc) func([]TValue) TAcc {
return func(data []TValue) TAcc {
out := initialAcc
for i, v := range data {
out = iterator(out, v, i)
}
return out
}
}
// Filter calls the predicate function on each element and removes those elements that do not satisfy the predicate
//
// Parameters
//
// • predicate: function that is called against each element
// • data: list of elements
//
// Return Value
//
// • result: list of elements where the predicate is not satisfied
//
func Filter[TValue any](predicate func(TValue) bool) func([]TValue) []TValue {
return func(data []TValue) []TValue {
out := make([]TValue, 0)
for _, v := range data {
if predicate(v) {
out = append(out, v)
}
}
return out
}
}
// Reject calls the predicate function on each element and removes those elements that do satisfy the predicate
//
// Parameters
//
// • predicate: function that is called against each element
// • data: list of elements
//
// Return Value
//
// • result: list of elements where the predicate is satisfied
//
func Reject[TValue any](predicate func(TValue) bool) func([]TValue) []TValue {
return func(data []TValue) []TValue {
out := make([]TValue, 0)
for _, v := range data {
if !predicate(v) {
out = append(out, v)
}
}
return out
}
}
// GroupBy creates a map where the key is a group identifier and the value is a slice with the elements that have the same identifer
//
// Parameters
//
// • grouper: function receives each element and returns a string identifier for the element
// • data: list of elements
//
// Return Value
//
// • result: map of elements grouped by identifiers
//
func GroupBy[TValue any](grouper func(TValue) string) func([]TValue) map[string][]TValue {
return func(data []TValue) map[string][]TValue{
var id string
out := make(map[string][]TValue)
for _, v := range data {
id = grouper(v)
out[id] = append(out[id], v)
}
return out
}
}
// Concat combines two slices of the same type together
//
// Parameters
//
// • slice: list of elements to concatenate
// • data: list of elements
//
// Return Value
//
// • result: map of elements grouped by identifiers
//
func Concat[TValue any](slice []TValue) func([]TValue) []TValue {
return func(data []TValue) []TValue{
return append(data, slice...)
}
}
// UniqBy returns a slice of only unique values based on a string identifier
//
// Parameters
//
// • identify: function that returns a string representation to uniquely identify the element
// • data: list of elements
//
// Return Value
//
// • result: list of unique elements
//
func UniqBy[TValue any](identify func(TValue) string) func([]TValue) []TValue {
return func(data []TValue) []TValue{
var id string
out := make([]TValue, 0)
identifiers := make(map[string]bool)
for _, v := range data {
id = identify(v)
if _, ok := identifiers[id]; !ok {
identifiers[id] = true
out = append(out, v)
}
}
return out
}
}
// Drop removes the first n elements from the slice and returns the remaining slice
//
// Parameters
//
// • count: number of elements to remove
// • data: list of elements
//
// Return Value
//
// • result: slice of remaining elements
//
func Drop[TValue any](count int) func([]TValue) []TValue {
return func(data []TValue) []TValue{
if count <=0 {
return data
}
if count >= len(data) {
return make([]TValue, 0)
}
return data[count:]
}
}
// DropLast removes the last n elements from the slice and returns the remaining slice
//
// Parameters
//
// • count: number of elements to remove
// • data: list of elements
//
// Return Value
//
// • result: slice of remaining elements
//
func DropLast[TValue any](count int) func([]TValue) []TValue {
return func(data []TValue) []TValue{
if count <=0 {
return data
}
data_length := len(data)
if count >= data_length {
return make([]TValue, 0)
}
return data[:(data_length-count)]
}
}
// Take returns the first n elements of the slice
//
// Parameters
//
// • count: number of elements to keep
// • data: list of elements
//
// Return Value
//
// • result: slice of remaining elements
//
func Take[TValue any](count int) func([]TValue) []TValue {
return func(data []TValue) []TValue{
if count >= len(data) || count <=0 {
return data
}
return data[:count]
}
}
// TakeLast returns the last n elements of the slice
//
// Parameters
//
// • count: number of elements to keep
// • data: list of elements
//
// Return Value
//
// • result: slice of remaining elements
//
func TakeLast[TValue any](count int) func([]TValue) []TValue {
return func(data []TValue) []TValue{
data_length := len(data)
if count >= data_length || count <=0 {
return data
}
return data[(data_length-count):]
}
}
// Flatten creates a new slice where one level of nested elements are unnested
//
// Parameters
//
// • data: list of elements
//
// Return Value
//
// • result: unnested list slice of elements
//
func Flatten[TValue any](data [][]TValue) []TValue {
out := make([]TValue, 0)
for _, v := range data {
out = append(out, v...)
}
return out
}
// SortBy applies a comparator against each element to sort the slice
//
// Parameters
//
// • comparator: function used to compare two elements in a slice
// • data: list of elements
//
// Return Value
//
// • result: sorted slice of elements
//
func SortBy[TValue any](comparator func(TValue, TValue) bool) func([]TValue) []TValue {
return func(data []TValue) []TValue {
out := data
sort.SliceStable(out, func(prevIndex int, nextIndex int) bool {
return comparator(data[prevIndex], data[nextIndex])
})
return out
}
}
// Append returns the slice with the additional element added to the end
//
// Parameters
//
// • element: element to add
// • data: list of elements
//
// Return Value
//
// • result: slice of elements with the additional element
//
func Append[TValue any](element TValue) func([]TValue) []TValue {
return func(data []TValue) []TValue {
out := append(data, element)
return out
}
}
// Prepend returns the slice with the additional element added to the beggining
//
// Parameters
//
// • element: element to add
// • data: list of elements
//
// Return Value
//
// • result: slice of elements with the additional element
//
func Prepend[TValue any](element TValue) func([]TValue) []TValue {
return func(data []TValue) []TValue {
out := append([]TValue{element}, data...)
return out
}
}
// Partition splits elements into two groups - one where the predicate is satisfied and one where the predicate is not
//
// Parameters
//
// • predicate: function that is called against each element returning true or false
// • data: list of elements
//
// Return Value
//
// • result: slice with two nested slices where the first are all elements satisfying the predicate and the second where all elements do not satisfy the predicate
//
func Partition[TValue any](predicate func(TValue) bool) func([]TValue) [][]TValue {
return func(data []TValue) [][]TValue {
outTrue := make([]TValue, 0)
outFalse := make([]TValue, 0)
for _, v := range data {
if predicate(v) {
outTrue = append(outTrue, v)
} else {
outFalse = append(outFalse, v)
}
}
return [][]TValue{outTrue, outFalse}
}
}
// Tail returns the input slice with all elements except the first element
//
// Parameters
//
// • data: list of elements
//
// Return Value
//
// • result: slice without the first element
//
func Tail[TValue any](data []TValue) []TValue {
if len(data) <= 1 {
return []TValue{}
}
return data[1:]
}
// Head returns first element of a slice
//
// Parameters
//
// • data: list of elements
//
// Return Value
//
// • result: first element in a slice
//
func Head[TValue any](data []TValue) TValue {
var out TValue
if len(data) <= 0 {
return out
}
out = data[:1][0]
return out
}
// SplitEvery returns elements in equal length slices
//
// Parameters
//
// • size: number of elements per chunk
// • data: list of elements
//
// Return Value
//
// • result: slice with sub-slices of specified length
func SplitEvery[TValue any](size int) func([]TValue) [][]TValue {
return func(data []TValue) [][]TValue {
if size <= 0 || len(data) <= 1 {
return [][]TValue{data}
}
out := make([][]TValue, 0)
currentGroup := make([]TValue, 0)
for i, v := range data {
if len(currentGroup) < size {
currentGroup = append(currentGroup, v)
} else {
out = append(out, currentGroup)
currentGroup = []TValue{v}
}
if (i + 1) >= len(data) {
out = append(out, currentGroup)
}
}
return out
}
}