-
Notifications
You must be signed in to change notification settings - Fork 0
/
funcy.go
400 lines (395 loc) · 9.9 KB
/
funcy.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
// Deprecated: use github.com/mdw-go/funcy/ranger instead.
package funcy
import (
"fmt"
"math/rand"
"reflect"
"sort"
)
func Is[T any](v any) bool {
_, ok := v.(T)
return ok
}
func As[T any](v any) T {
t, _ := v.(T)
return t
}
func FilterAs[T any](collection []any) []T {
return Map(As[T], Filter(Is[T], collection))
}
func Filter[T any](predicate func(t T) bool, values []T) (result []T) {
for _, value := range values {
if predicate(value) {
result = append(result, value)
}
}
return result
}
func Remove[T any](predicate func(t T) bool, values []T) []T {
return Filter(Complement(predicate), values)
}
func Complement[T any](predicate func(t T) bool) func(t T) bool {
return func(t T) bool { return !predicate(t) }
}
func MapVoid[T any](f func(T), values []T) {
for _, value := range values {
f(value)
}
}
func Map[I, O any](transform func(i I) O, values []I) (result []O) {
for _, value := range values {
result = append(result, transform(value))
}
return result
}
func Map2[Ia, Ib, O any](transform func(Ia, Ib) O, a []Ia, b []Ib) (result []O) {
length := len(a)
if len(b) < len(a) {
length = len(b)
}
for x := 0; x < length; x++ {
result = append(result, transform(a[x], b[x]))
}
return result
}
func MapCat[I, O any](transform func(i I) []O, values []I) (result []O) {
for _, value := range values {
result = append(result, transform(value)...)
}
return result
}
func MapAsAny[T any](items []T) (result []any) {
for _, item := range items {
result = append(result, item)
}
return result
}
func Reduce[T any](calc func(a, b T) T, start T, values []T) (result T) {
result = start
for _, next := range values {
result = calc(result, next)
}
return result
}
func Sum[T Number](rolls []T) T {
return Reduce(Add[T], 0, rolls)
}
func Add[T Number](a, b T) T {
return a + b
}
func Product[T Number](rolls []T) T {
return Reduce(Multiply[T], 1, rolls)
}
func Multiply[T Number](a, b T) T {
return a * b
}
func Min[T LessThan](all []T) T {
MIN := all[0]
for _, a := range all[1:] {
if a < MIN {
MIN = a
}
}
return MIN
}
func Max[T LessThan](all []T) T {
MAX := all[0]
for _, a := range all[1:] {
if a > MAX {
MAX = a
}
}
return MAX
}
func Abs[T Number](t T) T {
if t < 0 {
return -t
}
return t
}
func Square[T Number](t T) T { return t * t }
func IsZero[T Integer](t T) bool { return t == 0 }
func IsPositive[T Integer](t T) bool { return t > 0 }
func IsNegative[T Integer](t T) bool { return t < 0 }
func IsEven[T Integer](t T) bool { return t%2 == 0 }
func IsOdd[T Integer](t T) bool { return t%2 == 1 }
func IsNil[T any](t T) bool {
switch v := reflect.ValueOf(t); v.Kind() {
case reflect.Chan, reflect.Func, reflect.Map, reflect.Pointer, reflect.UnsafePointer, reflect.Interface, reflect.Slice:
return v.IsNil()
default:
return false
}
}
func String[T any](t T) string { return fmt.Sprint(t) }
func ByLength[T any](t T) int { return reflect.ValueOf(t).Len() }
func ByNumericValue[T Number](t T) T { return t }
func Range[N Number](start, stop N) (result []N) {
return RangeStep(start, stop, 1)
}
func RangeStep[N Number](start, stop, step N) (result []N) {
var (
compare func(start, stop N) bool
next func(start, step N) N
)
if start < stop {
compare = func(start, stop N) bool { return start < stop }
next = func(start, step N) N { return start + step }
} else {
compare = func(start, stop N) bool { return start > stop }
next = func(start, step N) N { return start - step }
}
for compare(start, stop) {
result = append(result, start)
start = next(start, step)
}
return result
}
func Take[T any](n int, values []T) []T {
if n >= len(values) {
n = len(values)
}
return values[:n]
}
func Drop[T any](n int, values []T) []T {
if n >= len(values) {
n = len(values)
}
return values[n:]
}
func First[T any](values []T) T {
return values[0]
}
func FirstOrDefault[T any](values []T) (default_ T) {
if len(values) == 0 {
return default_
}
return First(values)
}
func FirstNonDefault[T comparable](values ...T) (zero T) {
for _, value := range values {
if value != zero {
return value
}
}
return zero
}
func Last[T any](values []T) T {
return values[len(values)-1]
}
func LastOrDefault[T any](values []T) (default_ T) {
if len(values) == 0 {
return default_
}
return Last(values)
}
func Rest[T any](values []T) []T {
return Drop(1, values)
}
func DropAllBut[T any](n int, values []T) []T {
return Drop(len(values)-n, values)
}
func TakeAllBut[T any](n int, values []T) []T {
return Take(len(values)-n, values)
}
func TakeWhile[T any](predicate func(T) bool, values []T) (result []T) {
for _, value := range values {
if !predicate(value) {
break
}
result = append(result, value)
}
return result
}
func DropWhile[T any](predicate func(T) bool, values []T) (result []T) {
for _, value := range values {
if predicate(value) {
continue
}
result = append(result, value)
}
return result
}
func IndexBy[K comparable, V any](key func(V) K, list []V) map[K]V {
result := make(map[K]V)
for _, value := range list {
result[key(value)] = value
}
return result
}
func KeyValueIndexBy[K comparable, V1, V2 any](kv func(V1) (K, V2), list []V1) map[K]V2 {
result := make(map[K]V2)
for _, item := range list {
key, value := kv(item)
result[key] = value
}
return result
}
func SlicedIndexBy[K comparable, V any](key func(V) K, list []V) map[K][]V {
result := make(map[K][]V)
for _, value := range list {
key := key(value)
result[key] = append(result[key], value)
}
return result
}
func SlicedKeyValueIndexBy[K comparable, V1, V2 any](kv func(V1) (K, V2), list []V1) map[K][]V2 {
result := make(map[K][]V2)
for _, item := range list {
key, value := kv(item)
result[key] = append(result[key], value)
}
return result
}
func GroupBy[K comparable, V any](key func(V) K, list []V) (result [][]V) {
// TODO: return maps.Values(SlicedIndexBy(key, list))
// (but only once the maps package moves from golang.org/x/exp into the std lib)
index := SlicedIndexBy(key, list)
for _, value := range index {
result = append(result, value)
}
return result
}
func Drain[T any](channel <-chan T) (slice []T) {
for item := range channel {
slice = append(slice, item)
}
return slice
}
func Load[T any](result chan<- T, stream []T) {
defer close(result)
for _, item := range stream {
result <- item
}
}
func Shuffle[T any](values []T) (result []T) {
index := Range(0, len(values))
rand.Shuffle(len(index), func(i, j int) { index[i], index[j] = index[j], index[i] })
for x := 0; x < len(values); x++ {
result = append(result, values[index[x]])
}
return result
}
func Reverse[T any](values []T) (result []T) {
for x := len(values) - 1; x >= 0; x-- {
result = append(result, values[x])
}
return result
}
func SortAscending[C LessThan, V any](key func(V) C, original []V) (result []V) {
collection := make([]V, len(original))
copy(collection, original)
sort.Slice(collection, func(i, j int) bool { return key(collection[i]) < key(collection[j]) })
return collection
}
func SortDescending[C LessThan, V any](key func(V) C, original []V) (result []V) {
collection := make([]V, len(original))
copy(collection, original)
sort.Slice(collection, func(i, j int) bool { return key(collection[i]) > key(collection[j]) })
return collection
}
func Frequencies[T comparable](values []T) map[T]int {
result := make(map[T]int)
for _, v := range values {
result[v]++
}
return result
}
func Flatten[T any](matrix [][]T) (result []T) {
for _, row := range matrix {
result = append(result, row...)
}
return result
}
func Concat[T any](slices ...[]T) (result []T) {
return Flatten(slices)
}
func Partition[T any](length, increment int, values []T) (result [][]T) {
for x := 0; x+length <= len(values); x += increment {
result = append(result, values[x:x+length])
}
return result
}
func Zip[A, B any](a []A, b []B) (result []Pair[A, B]) {
length := len(a)
if len(b) < len(a) {
length = len(b)
}
for x := 0; x < length; x++ {
result = append(result, Pair[A, B]{A: a[x], B: b[x]})
}
return result
}
func ZipMap[A comparable, B any](a []A, b []B) (result map[A]B) {
length := len(a)
if len(b) < len(a) {
length = len(b)
}
result = make(map[A]B, length)
for x := 0; x < length; x++ {
result[a[x]] = b[x]
}
return result
}
func Repeat[T any](n int, t T) (result []T) {
for ; n > 0; n-- {
result = append(result, t)
}
return result
}
func All(inputs []bool) bool { return len(TakeWhile(isTrue, inputs)) == len(inputs) }
func Any(inputs []bool) bool { return len(DropWhile(Complement(isTrue), inputs)) > 0 }
func None(inputs []bool) bool { return len(TakeWhile(Complement(isTrue), inputs)) == len(inputs) }
func isTrue(b bool) bool { return b }
func Unique[T comparable](values []T) []T {
m := make(map[T]struct{})
for _, v := range values {
m[v] = struct{}{}
}
return MapKeys(m)
}
func MapKeys[K comparable, V any](m map[K]V) (results []K) {
results = make([]K, 0, len(m))
for key := range m {
results = append(results, key)
}
return results
}
func MapValues[K comparable, V any](m map[K]V) (results []V) {
results = make([]V, 0, len(m))
for _, value := range m {
results = append(results, value)
}
return results
}
func MapPairs[K comparable, V any](m map[K]V) (results []Pair[K, V]) {
results = make([]Pair[K, V], 0, len(m))
for key, value := range m {
results = append(results, Pair[K, V]{A: key, B: value})
}
return results
}
func PairsMap[K comparable, V any](pairs []Pair[K, V]) map[K]V {
result := make(map[K]V, len(pairs))
for _, p := range pairs {
result[p.A] = p.B
}
return result
}
func MapLookup[K comparable, V any](m map[K]V) func(K) V { return func(k K) V { return m[k] } }
func SliceLookup[V any](s []V) func(int) V { return func(i int) V { return s[i] } }
type Pair[A, B any] struct {
A A
B B
}
type (
Number interface {
Integer | uintptr | float64 | float32
}
Integer interface {
int | int8 | int16 | int32 | int64 | uint | uint8 | uint16 | uint32 | uint64
}
LessThan interface {
Number | string
}
)