-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaps.go
More file actions
84 lines (73 loc) · 1.95 KB
/
Copy pathmaps.go
File metadata and controls
84 lines (73 loc) · 1.95 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
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
package iterator
// Keys is a modifier that takes a map iterator and returns an iterator for the keys
func Keys[K comparable, V any](iter Iterator[KV[K, V]]) Iterator[K] {
return Map(func(_ int, item KV[K, V]) (K, error) {
return item.Key, nil
})(iter)
}
// Values is a modifier that takes a map iterator and returns an iterator for the values
func Values[K comparable, V any](iter Iterator[KV[K, V]]) Iterator[V] {
return Map(func(_ int, item KV[K, V]) (V, error) {
return item.Val, nil
})(iter)
}
// Assoc the given keys to the given values in a new map iterator
func Assoc[K comparable, V any](keys Iterator[K], values Iterator[V]) Iterator[KV[K, V]] {
var hasMore bool
var item KV[K, V]
var err error
return &iterator[KV[K, V]]{
next: func() bool {
hasMore = keys.Next() && values.Next()
if !hasMore {
return hasMore
}
var key K
key, err = keys.Get()
if err != nil {
return false
}
var value V
value, err = values.Get()
if err != nil {
return false
}
item = KV[K, V]{Key: key, Val: value}
return true
},
get: func() (KV[K, V], error) {
if !hasMore || err != nil {
return *new(KV[K, V]), err
}
return item, nil
},
close: func() error {
if err := keys.Close(); err != nil {
values.Close()
return err
}
return values.Close()
},
err: func() error {
if err != nil {
return err
}
if err := keys.Err(); err != nil {
return err
}
return values.Err()
},
}
}
// AssocKeys returns a modifier that associates the iterator to the given keys
func AssocKeys[K comparable, V any](keys Iterator[K]) Modifier[V, KV[K, V]] {
return func(values Iterator[V]) Iterator[KV[K, V]] {
return Assoc(keys, values)
}
}
// AssocValues returns a modifier that associates the iterator to the given values
func AssocValues[K comparable, V any](values Iterator[V]) Modifier[K, KV[K, V]] {
return func(keys Iterator[K]) Iterator[KV[K, V]] {
return Assoc(keys, values)
}
}