-
Notifications
You must be signed in to change notification settings - Fork 0
/
slice.go
395 lines (345 loc) · 13 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
package slice
import (
"math/rand"
"sort"
"time"
"golang.org/x/exp/constraints"
)
type Reduction int
const (
Cont Reduction = iota
Halt
)
type Order int
const (
Asc Order = iota
Desc
)
type pair[Element any] struct {
left Element
right Element
}
type Number interface {
constraints.Integer | constraints.Float
}
// All returns true if fun returns true for all elements in the slice.
func All[Element any](elements []Element, fun func(Element) bool) bool {
return ReduceWhile(elements, func(element Element, accumulator bool) (Reduction, bool) {
if fun(element) {
return Cont, true
}
return Halt, false
}, false)
}
// Any returns true if fun returns true for at least one element in the slice.
func Any[Element any](elements []Element, fun func(Element) bool) bool {
return ReduceWhile(elements, func(element Element, accumulator bool) (Reduction, bool) {
if fun(element) {
return Halt, true
}
return Cont, false
}, false)
}
// At finds the element at the given index (zero-based).
func At[Element any](elements []Element, index int, defaultValue Element) Element {
if index >= 0 && index < len(elements) {
return elements[index]
} else {
return defaultValue
}
}
// Concat concatenates the enumerable on the right with the enumerable on the left.
func Concat[Element any](left []Element, right []Element) []Element {
return append(left, right...)
}
// Count counts the number of elements in the slice.
func Count[Element any](elements []Element) int {
return len(elements)
}
// CountBy counts the number of elements in slice where fun returns true.
func CountBy[Element any](elements []Element, fun func(Element) bool) int {
return Reduce(elements, func(element Element, accumulator int) int {
if fun(element) {
return accumulator + 1
}
return accumulator
}, 0)
}
// Each invokes fun on each element in the slice.
func Each[Element any](elements []Element, fun func(Element)) {
Reduce(elements, func(element Element, accumulator interface{}) interface{} {
fun(element)
return accumulator
}, nil)
}
// Filter returns elements where fun returns true.
func Filter[Element any](elements []Element, fun func(Element) bool) []Element {
return Reduce(elements, func(element Element, accumulator []Element) []Element {
if fun(element) {
return append(accumulator, element)
}
return accumulator
}, make([]Element, 0))
}
// FlatMap maps the given fun over slice and flattens the result.
func FlatMap[Element any](elements []Element, fun func(Element) []Element) []Element {
return Reduce(elements, func(element Element, accumulator []Element) []Element {
return append(accumulator, fun(element)...)
}, make([]Element, 0))
}
// Frequencies returns a map with keys as unique elements and values as the count of every element.
func Frequencies[Element comparable](elements []Element) map[Element]int {
return FrequenciesBy(elements, func(element Element) Element {
return element
})
}
// FrequenciesBy returns a map with keys as unique elements given by key_fun and values as the count of every element.
func FrequenciesBy[Element any, Key comparable](elements []Element, fun func(Element) Key) map[Key]int {
return Reduce(elements, func(element Element, accumulator map[Key]int) map[Key]int {
accumulator[fun(element)]++
return accumulator
}, make(map[Key]int))
}
// GroupBy splits the slice into groups based on key_fun.
func GroupBy[Element any, GroupBy comparable](elements []Element, fun func(Element) GroupBy) map[GroupBy][]Element {
return Reduce(elements, func(element Element, accumulator map[GroupBy][]Element) map[GroupBy][]Element {
accumulator[fun(element)] = append(accumulator[fun(element)], element)
return accumulator
}, make(map[GroupBy][]Element))
}
// IsMember checks if element exists in the slice.
func IsMember[Element comparable](elements []Element, member Element) bool {
return IsMemberBy(elements, member, func(element Element) Element {
return element
})
}
// IsMemberBy checks if element exists in the slice according to fun.
func IsMemberBy[Element any, IsMemberBy comparable](elements []Element, member Element, fun func(Element) IsMemberBy) bool {
return ReduceWhile(elements, func(element Element, accumulator bool) (Reduction, bool) {
if fun(element) == fun(member) {
return Halt, true
}
return Cont, false
}, false)
}
// Map invokes fun on each element in the slice.
func Map[Element any, ReturnElement any](elements []Element, fun func(Element) ReturnElement) []ReturnElement {
return Reduce(elements, func(element Element, accumulator []ReturnElement) []ReturnElement {
return append(accumulator, fun(element))
}, make([]ReturnElement, 0))
}
// Max returns the maximum element in the slice.
func Max[Element constraints.Ordered](elements []Element) Element {
return MaxBy(elements, func(element Element) Element {
return element
})
}
// MaxBy returns the maximum element in the slice according to fun.
func MaxBy[Element any, CompareBy constraints.Ordered](elements []Element, fun func(Element) CompareBy) Element {
return Reduce(elements, func(element Element, max Element) Element {
if fun(element) > fun(max) {
max = element
}
return max
}, elements[0])
}
// Min returns the minimum element in the slice.
func Min[Element constraints.Ordered](elements []Element) Element {
return MinBy(elements, func(element Element) Element {
return element
})
}
// MinBy returns the minimum element in the slice according to fun.
func MinBy[Element any, CompareBy constraints.Ordered](elements []Element, fun func(Element) CompareBy) Element {
return Reduce(elements, func(element Element, min Element) Element {
if fun(element) < fun(min) {
min = element
}
return min
}, elements[0])
}
// MinMax returns the minimum and maximum element in the slice.
func MinMax[Element constraints.Ordered](elements []Element) (Element, Element) {
return MinMaxBy(elements, func(element Element) Element {
return element
})
}
// MinMaxBy returns the minimum and maximum element in the slice according to fun.
func MinMaxBy[Element any, CompareBy constraints.Ordered](elements []Element, fun func(Element) CompareBy) (Element, Element) {
result := Reduce(elements, func(element Element, accumulator pair[Element]) pair[Element] {
if fun(element) < fun(accumulator.left) {
accumulator.left = element
}
if fun(element) > fun(accumulator.right) {
accumulator.right = element
}
return accumulator
}, pair[Element]{left: elements[0], right: elements[0]})
return result.left, result.right
}
// Product returns the product of all elements.
func Product[Element Number](elements []Element) Element {
return ProductBy(elements, func(element Element) Element {
return element
})
}
// ProductBy returns the product of all elements according to fun.
func ProductBy[Element any, Product Number](elements []Element, fun func(Element) Product) Product {
return Reduce(elements[1:], func(element Element, accumulator Product) Product {
return fun(element) * accumulator
}, fun(elements[0]))
}
// Random returns a random element from the slice.
func Random[Element any](elements []Element, seed ...int64) Element {
if len(seed) == 0 {
rand.Seed(time.Now().UTC().UnixNano())
} else if len(seed) == 1 {
rand.Seed(seed[0])
} else {
panic("unexpected value for seed parameter")
}
return elements[rand.Intn(len(elements))]
}
// Reduce invokes fun on each element in the slice with the accumulator.
func Reduce[Element any, Accumulator any](elements []Element, fun func(Element, Accumulator) Accumulator, accumulator Accumulator) Accumulator {
return ReduceWhile(elements, func(element Element, accumulator Accumulator) (Reduction, Accumulator) {
return Cont, fun(element, accumulator)
}, accumulator)
}
// ReduceWhile invokes fun on each element in the slice with the accumulator until Halt is returned.
func ReduceWhile[Element any, Accumulator any](elements []Element, fun func(Element, Accumulator) (Reduction, Accumulator), accumulator Accumulator) Accumulator {
reduction := Cont
for _, element := range elements {
reduction, accumulator = fun(element, accumulator)
if reduction == Halt {
return accumulator
}
}
return accumulator
}
// Reject returns elements excluding those where fun returns true.
func Reject[Element any](elements []Element, fun func(Element) bool) []Element {
return Reduce(elements, func(element Element, accumulator []Element) []Element {
if !fun(element) {
return append(accumulator, element)
}
return accumulator
}, make([]Element, 0))
}
// Reverse returns a slice of elements in reverse order.
func Reverse[Element comparable](elements []Element) []Element {
return Reduce(elements, func(element Element, accumulator []Element) []Element {
return append([]Element{element}, accumulator...)
}, make([]Element, 0))
}
// Shuffle returns a list with the elements of the slice shuffled.
func Shuffle[Element any](elements []Element, seed ...int64) []Element {
if len(seed) == 0 {
rand.Seed(time.Now().UTC().UnixNano())
} else if len(seed) == 1 {
rand.Seed(seed[0])
} else {
panic("unexpected value for seed parameter")
}
shuffledElements := make([]Element, len(elements))
copy(shuffledElements, elements)
rand.Shuffle(len(elements), func(i, j int) {
shuffledElements[i], shuffledElements[j] = shuffledElements[j], shuffledElements[i]
})
return shuffledElements
}
// Sort returns a slice sorted according to fun.
func Sort[Element constraints.Ordered](elements []Element, order Order) []Element {
return SortBy(elements, func(element Element) Element {
return element
}, order)
}
// SortBy returns a slice sorted according to fun.
func SortBy[Element any, SortBy constraints.Ordered](elements []Element, fun func(Element) SortBy, order Order) []Element {
sortedElements := make([]Element, len(elements))
copy(sortedElements, elements)
sort.Slice(sortedElements, func(i, j int) bool {
if order == Asc {
return fun(sortedElements[i]) < fun(sortedElements[j])
}
return fun(sortedElements[i]) > fun(sortedElements[j])
})
return sortedElements
}
// SplitWhile splits the slice in two at the position of the element for which fun returns a false for the first time.
func SplitWhile[Element any](elements []Element, fun func(Element) bool) ([]Element, []Element) {
addToLeft := true
result := Reduce(elements, func(element Element, accumulator pair[[]Element]) pair[[]Element] {
if addToLeft == true && fun(element) == false {
addToLeft = false
}
if addToLeft {
accumulator.left = append(accumulator.left, element)
} else {
accumulator.right = append(accumulator.right, element)
}
return accumulator
}, pair[[]Element]{})
return result.left, result.right
}
// SplitWith splits the slice in two lists according to the given function fun.
func SplitWith[Element any](elements []Element, fun func(Element) bool) ([]Element, []Element) {
result := Reduce(elements, func(element Element, accumulator pair[[]Element]) pair[[]Element] {
if fun(element) {
accumulator.left = append(accumulator.left, element)
} else {
accumulator.right = append(accumulator.right, element)
}
return accumulator
}, pair[[]Element]{})
return result.left, result.right
}
// Sum returns the sum of all elements.
func Sum[Element Number](elements []Element) Element {
return SumBy(elements, func(element Element) Element {
return element
})
}
// SumBy returns the sum of all elements according to fun.
func SumBy[Element any, SumBy Number](elements []Element, fun func(Element) SumBy) SumBy {
return Reduce(elements[1:], func(element Element, accumulator SumBy) SumBy {
return fun(element) + accumulator
}, fun(elements[0]))
}
// Take takes an amount of elements from the beginning of the slice.
func Take[Element any](elements []Element, amount uint) []Element {
return ReduceWhile(elements, func(element Element, accumulator []Element) (Reduction, []Element) {
if len(accumulator) < int(amount) {
accumulator = append(accumulator, element)
return Cont, accumulator
}
return Halt, accumulator
}, make([]Element, 0))
}
// TakeWhile takes the elements from the beginning of the slice while fun returns a truthy value.
func TakeWhile[Element any](elements []Element, fun func(Element) bool) []Element {
return ReduceWhile(elements, func(element Element, accumulator []Element) (Reduction, []Element) {
if fun(element) {
return Cont, append(accumulator, element)
}
return Halt, accumulator
}, make([]Element, 0))
}
// Uniq iterates over the slice, removing all duplicated elements.
func Uniq[Element comparable](elements []Element) []Element {
return Reduce(elements, func(element Element, accumulator []Element) []Element {
if !IsMember(accumulator, element) {
accumulator = append(accumulator, element)
}
return accumulator
}, make([]Element, 0))
}
// UniqBy iterates over the slice, removing all duplicated elements according to fun.
func UniqBy[Element any, UniqBy comparable](elements []Element, fun func(Element) UniqBy) []Element {
return Reduce(elements, func(element Element, accumulator []Element) []Element {
if !IsMemberBy(accumulator, element, fun) {
accumulator = append(accumulator, element)
}
return accumulator
}, make([]Element, 0))
}