-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmap.go
More file actions
35 lines (26 loc) · 683 Bytes
/
Copy pathmap.go
File metadata and controls
35 lines (26 loc) · 683 Bytes
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
package easy
// Keys returns a slice of keys of the map.
func Keys[K comparable, V any](m map[K]V) []K {
keys := make([]K, 0, len(m))
for key := range m {
keys = append(keys, key)
}
return keys
}
// Values returns a slice of values of the map.
func Values[K comparable, V any](m map[K]V) []V {
values := make([]V, 0, len(m))
for _, val := range m {
values = append(values, val)
}
return values
}
// SliceToMap returns a map of converted slice elements.
func SliceToMap[T any, K comparable, V any](s []T, toMapFunc func(e T) (K, V)) map[K]V {
m := make(map[K]V, len(s))
for _, element := range s {
key, val := toMapFunc(element)
m[key] = val
}
return m
}