Skip to content

Commit c8073c8

Browse files
author
calvin
committed
map util function library
1 parent f863213 commit c8073c8

File tree

3 files changed

+138
-0
lines changed

3 files changed

+138
-0
lines changed

intersect.go

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package sugar
2+
3+
func Contains[T comparable](collection []T, element T) bool {
4+
for _, t := range collection {
5+
if t == element {
6+
return true
7+
}
8+
}
9+
return false
10+
}

map.go

+122
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
package sugar
2+
3+
func Keys[K comparable, V any](in map[K]V) []K {
4+
result := make([]K, 0, len(in))
5+
6+
for k := range in {
7+
result = append(result, k)
8+
}
9+
10+
return result
11+
}
12+
13+
func Values[K comparable, V any](in map[K]V) []V {
14+
result := make([]V, 0, len(in))
15+
16+
for _, v := range in {
17+
result = append(result, v)
18+
}
19+
20+
return result
21+
}
22+
23+
func FiltrateBy[K comparable, V any](in map[K]V, fileter func(K, V) bool) map[K]V {
24+
result := map[K]V{}
25+
26+
for k, v := range in {
27+
if fileter(k, v) {
28+
result[k] = v
29+
}
30+
}
31+
32+
return result
33+
}
34+
35+
func FiltrateByKeys[K comparable, V any](in map[K]V, keys []K) map[K]V {
36+
result := map[K]V{}
37+
38+
for k, v := range in {
39+
if Contains(keys, k) {
40+
result[k] = v
41+
}
42+
}
43+
44+
return result
45+
}
46+
47+
func FiltrateByValues[K comparable, V comparable](in map[K]V, values []V) map[K]V {
48+
result := map[K]V{}
49+
50+
for k, v := range in {
51+
if Contains(values, v) {
52+
result[k] = v
53+
}
54+
}
55+
56+
return result
57+
}
58+
59+
func MapToEntries[K comparable, V any](in map[K]V) []Entry[K, V] {
60+
result := make([]Entry[K, V], 0, len(in))
61+
62+
for k, v := range in {
63+
result = append(result, Entry[K, V]{k, v})
64+
}
65+
66+
return result
67+
}
68+
69+
func EntriesToMap[K comparable, V any](entries []Entry[K, V]) map[K]V {
70+
result := map[K]V{}
71+
72+
for _, entry := range entries {
73+
result[entry.Key] = entry.Value
74+
}
75+
76+
return result
77+
}
78+
79+
func Invert[K comparable, V comparable](in map[K]V) map[V]K {
80+
result := map[V]K{}
81+
82+
for k, v := range in {
83+
result[v] = k
84+
}
85+
86+
return result
87+
}
88+
89+
// Assign merges multiple maps from left to right.
90+
func Assign[K comparable, V any](maps ...map[K]V) map[K]V {
91+
result := map[K]V{}
92+
93+
for _, m := range maps {
94+
for k, v := range m {
95+
result[k] = v
96+
}
97+
}
98+
99+
return result
100+
}
101+
102+
// MapUpdateKeys manipulates a map keys and transforms it to a map of another type.
103+
func MapUpdateKeys[K comparable, V any, R comparable](in map[K]V, iteratee func(K, V) R) map[R]V {
104+
result := map[R]V{}
105+
106+
for k, v := range in {
107+
result[iteratee(k, v)] = v
108+
}
109+
110+
return result
111+
}
112+
113+
// MapUpdateValues manipulates a map values and transforms it to a map of another type.
114+
func MapUpdateValues[K comparable, V any, R any](in map[K]V, iteratee func(K, V) R) map[K]R {
115+
result := map[K]R{}
116+
117+
for k, v := range in {
118+
result[k] = iteratee(k, v)
119+
}
120+
121+
return result
122+
}

tuple.go

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package sugar
2+
3+
type Entry[K comparable, V any] struct {
4+
Key K
5+
Value V
6+
}

0 commit comments

Comments
 (0)