-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmap.go
85 lines (71 loc) · 1.93 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
package maps
import "iter"
// Keys returns a slice of all keys in m
func Keys[Map ~map[K]V, K comparable, V any](m Map) iter.Seq[K] {
return func(yield func(K) bool) {
for k := range m {
if !yield(k) {
return
}
}
}
}
// Values returns a slice of all values in m
func Values[Map ~map[K]V, K comparable, V any](m Map) iter.Seq[V] {
return func(yield func(V) bool) {
for _, v := range m {
if !yield(v) {
return
}
}
}
}
// Union returns a new map of all key/value pairs in left and right. If a key exists
// in both left and right the value in right will appear in the resultant map.
func Union[Map ~map[K]V, K comparable, V any](left, right Map) Map {
result := make(Map)
for k, v := range left {
result[k] = v
}
for k, v := range right {
result[k] = v
}
return result
}
// UnionInPlace modifies left to include key/value pairs in right. If a key exists
// in both left and right the value in right will be used.
func UnionInPlace[Map ~map[K]V, K comparable, V any](left, right Map) Map {
for k, v := range right {
left[k] = v
}
return left
}
// Intersect returns a new map of key/value pairs where the key exists in both left and right.
// The value from right will be used in the return map.
func Intersect[Map ~map[K]V, K comparable, V any](left, right Map) Map {
result := make(Map)
for k := range left {
if rv, ok := right[k]; ok {
result[k] = rv
}
}
return result
}
// Difference returns a new map of key/value pairs that only appear in left.
func Difference[Map ~map[K]V, K comparable, V any](left, right Map) Map {
result := make(Map)
for k, v := range left {
if _, ok := right[k]; !ok {
result[k] = v
}
}
return result
}
// YieldAll iterates over a provided iterator and returns a Map containing all elements from the iterator.
func YieldAll[Map ~map[K]V, K comparable, V any](i iter.Seq2[K, V]) Map {
result := make(Map)
for k, v := range i {
result[k] = v
}
return result
}