-
Notifications
You must be signed in to change notification settings - Fork 1
/
map.go
126 lines (115 loc) · 3.52 KB
/
map.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
package goneric
// Map maps the list of variadic(...) values via function.
// It is provided as convenience, MapSlice() should be used when you have incoming slice
func Map[T1, T2 any](mapFunc func(v T1) T2, slice ...T1) []T2 {
out := make([]T2, len(slice))
for idx, v := range slice {
out[idx] = mapFunc(v)
}
return out
}
// MapSlice maps slice using provided function
func MapSlice[T1, T2 any](mapFunc func(v T1) T2, slice []T1) []T2 {
out := make([]T2, len(slice))
for idx, v := range slice {
out[idx] = mapFunc(v)
}
return out
}
// MapErr maps the list of variadic(...) values via function. and returns on first error
// Returns slice with elements that didn't return error before the failure
// so index of the first element in error is essentially `slice[len(out)]`
func MapErr[T1, T2 any](mapFunc func(v T1) (T2, error), slice ...T1) (out []T2, err error) {
out = make([]T2, len(slice))
for idx, v := range slice {
out[idx], err = mapFunc(v)
if err != nil {
return out[:idx], err
}
}
return out, nil
}
// MapSliceErr maps slice using provided function, returning first error
// Returns slice with elements that didn't return error
func MapSliceErr[T1, T2 any](mapFunc func(v T1) (T2, error), slice []T1) (out []T2, err error) {
out = make([]T2, len(slice))
for idx, v := range slice {
out[idx], err = mapFunc(v)
if err != nil {
return out[:idx], err
}
}
return out, nil
}
// MapSliceSkip maps slice using provided function, allowing to skip entries by returning true
// Returns slice with elements that didn't return true
func MapSliceSkip[T1, T2 any](mapFunc func(v T1) (T2, bool), slice []T1) (out []T2) {
out = make([]T2, 0)
for _, v := range slice {
r, skip := mapFunc(v)
if !skip {
out = append(out, r)
}
}
return out
}
// MapSliceErrSkip maps slice using provided function, allowing to skip entries by returning ErrSkip
// Returns on first non `ErrSkip` error
func MapSliceErrSkip[T1, T2 any](mapFunc func(v T1) (T2, error), slice []T1) (out []T2, err error) {
out = make([]T2, 0)
for _, v := range slice {
r, err := mapFunc(v)
switch err.(type) {
case ErrSkip:
continue
case nil:
out = append(out, r)
default:
return out, err
}
}
return out, nil
}
// MapSliceKey returns keys of a map in slice
func MapSliceKey[K comparable, V any](in map[K]V) (out []K) {
out = make([]K, len(in))
i := 0
for k := range in {
out[i] = k
i++
}
return out
}
// MapSliceValue returns values of a map in slice
func MapSliceValue[K comparable, V any](in map[K]V) (out []V) {
out = make([]V, len(in))
i := 0
for _, v := range in {
out[i] = v
i++
}
return out
}
// MapToSlice converts map into slice via specified function
func MapToSlice[K1 comparable, V any, V2 any](f func(k K1, v V) V2, in map[K1]V) (out []V2) {
out = make([]V2, len(in))
i := 0
for k, v := range in {
out[i] = f(k, v)
i++
}
return out
}
// MapMap runs every map element thru function that returns new key and value, and returns that in another map. Types can vary between in and out.
func MapMap[K1, K2 comparable, V1, V2 any](mapFunc func(k K1, v V1) (K2, V2), in map[K1]V1) (out map[K2]V2) {
out = make(map[K2]V2, len(in))
MapMapInplace(mapFunc, in, out)
return out
}
// MapMapInplace runs every map element thru function that returns new key and value, and puts it into existing map. Types can vary between in and out.
func MapMapInplace[K1, K2 comparable, V1, V2 any](mapFunc func(k K1, v V1) (K2, V2), in map[K1]V1, out map[K2]V2) {
for kin, vin := range in {
kout, vout := mapFunc(kin, vin)
out[kout] = vout
}
}