-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmap.go
More file actions
59 lines (53 loc) · 1.18 KB
/
Copy pathmap.go
File metadata and controls
59 lines (53 loc) · 1.18 KB
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
package gdk
// MapClear remove all keys and values in map
func MapClear[K comparable, V any](data map[K]V) {
for k := range data {
delete(data, k)
}
}
// MapSize return count of size
func MapSize[K comparable, V any](data map[K]V) int {
if data == nil {
return 0
}
return len(data)
}
// MapRange calls f sequentially for each key and value present in the map.
func MapRange[K comparable, V any](data map[K]V, f BiFunc[bool, K, V]) {
for k, v := range data {
ok := f(k, v)
if !ok {
break
}
}
}
// MapFilter 过滤出符合条件的key,value
func MapFilter[K comparable, V any](data map[K]V, f BiFunc[bool, K, V]) map[K]V {
ret := make(map[K]V)
for k, v := range data {
ok := f(k, v)
if !ok {
continue
}
ret[k] = v
}
return ret
}
// MapValues return all value as slice in map
func MapValues[K comparable, V any](data map[K]V) []V {
ret := make([]V, 0)
MapRange(data, func(key K, value V) bool {
ret = append(ret, value)
return true
})
return ret
}
// MapKeys return all key as slice in map
func MapKeys[K comparable, V any](data map[K]V) []K {
ret := make([]K, 0)
MapRange(data, func(key K, value V) bool {
ret = append(ret, key)
return true
})
return ret
}