Source file
src/maps/iter.go
1
2
3
4
5 package maps
6
7 import "iter"
8
9
10
11
12 func All[Map ~map[K]V, K comparable, V any](m Map) iter.Seq2[K, V] {
13 return func(yield func(K, V) bool) {
14 for k, v := range m {
15 if !yield(k, v) {
16 return
17 }
18 }
19 }
20 }
21
22
23
24
25 func Keys[Map ~map[K]V, K comparable, V any](m Map) iter.Seq[K] {
26 return func(yield func(K) bool) {
27 for k := range m {
28 if !yield(k) {
29 return
30 }
31 }
32 }
33 }
34
35
36
37
38 func Values[Map ~map[K]V, K comparable, V any](m Map) iter.Seq[V] {
39 return func(yield func(V) bool) {
40 for _, v := range m {
41 if !yield(v) {
42 return
43 }
44 }
45 }
46 }
47
48
49
50 func Insert[Map ~map[K]V, K comparable, V any](m Map, seq iter.Seq2[K, V]) {
51 for k, v := range seq {
52 m[k] = v
53 }
54 }
55
56
57
58 func Collect[K comparable, V any](seq iter.Seq2[K, V]) map[K]V {
59 m := make(map[K]V)
60 Insert(m, seq)
61 return m
62 }
63
View as plain text