-
Notifications
You must be signed in to change notification settings - Fork 0
/
maps.go
51 lines (37 loc) · 1.01 KB
/
maps.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
package util
func ConvertMap[K1 comparable, V1 any, K2 comparable, V2 any](
m map[K1]V1, conv func(k K1, v V1) (K2, V2, error)) (map[K2]V2, error) {
converted := make(map[K2]V2, len(m))
for k, v := range m {
convertedK, convertedV, err := conv(k, v)
if err != nil {
return nil, err
}
converted[convertedK] = convertedV
}
return converted, nil
}
// MapKeys returns a slice []KeyType with the keys of the map[KeyType]ValueType object.
func MapKeys[KeyType comparable, ValueType any](m map[KeyType]ValueType) []KeyType {
l := make([]KeyType, len(m))
i := 0
for k := range m {
l[i] = k
i++
}
return l
}
// MapValues returns a slice []ValueType with the values of the map[KeyType]ValueType object.
func MapValues[KeyType comparable, ValueType any](m map[KeyType]ValueType) []ValueType {
l := make([]ValueType, len(m))
i := 0
for _, v := range m {
l[i] = v
i++
}
return l
}
func MapHas[KeyType comparable, ValueType any](m map[KeyType]ValueType, k KeyType) bool {
_, has := m[k]
return has
}