-
Notifications
You must be signed in to change notification settings - Fork 0
/
mfr.go
75 lines (70 loc) · 1.48 KB
/
mfr.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
package mfr
// Ctx is the context passed to Map/Filter/Reduce/ForEach
type Ctx[T any] struct {
// Array is the
Array []T
Index int
IsFirst bool
IsLast bool
Item T
}
// Filter returns an array of items for which the
// provided function returns true.
func Filter[T any](s []T, fn func(Ctx[T]) bool) []T {
to := make([]T, 0)
for i, v := range s {
ok := fn(Ctx[T]{
Array: s,
Index: i,
IsFirst: i == 0,
IsLast: i == (len(s) - 1),
Item: v,
})
if ok {
to = append(to, v)
}
}
return to
}
// ForEach iterates over each item of the provided array, calling the provided
// function for each item.
func ForEach[T any](s []T, fn func(Ctx[T])) {
for i, v := range s {
fn(Ctx[T]{
Array: s,
Index: i,
IsFirst: i == 0,
IsLast: i == (len(s) - 1),
Item: v,
})
}
}
// Map maps one type to another, via the transform function provided.
func Map[From any, To any](s []From, fn func(Ctx[From]) To) []To {
to := make([]To, len(s))
for i, v := range s {
to[i] = fn(Ctx[From]{
Array: s,
Index: i,
IsFirst: i == 0,
IsLast: i == (len(s) - 1),
Item: v,
})
}
return to
}
// Reduce reduces and/or collects from an array to a target type, from an
// initial value.
func Reduce[From any, To any](s []From, init To, fn func(Ctx[From], To) To) To {
acc := init
for i, v := range s {
acc = fn(Ctx[From]{
Array: s,
Index: i,
IsFirst: i == 0,
IsLast: i == (len(s) - 1),
Item: v,
}, acc)
}
return acc
}