-
Notifications
You must be signed in to change notification settings - Fork 0
/
typed_iterators.go
107 lines (99 loc) · 2.48 KB
/
typed_iterators.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
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package itertools
import (
"reflect"
"unicode/utf8"
)
// NewSliceIterator creates iterator for given slice,
// meaning iterator will yield all elements of the slice.
func NewSliceIterator[S ~[]T, T any](s S) *Iterator[T] {
var (
idx int
zero T
)
return New(func() (T, bool) {
if idx >= len(s) {
return zero, false
}
v := s[idx]
idx++
return v, true
})
}
// NewChanIterator creates iterator yielding values from channel
// until the channel is closed.
func NewChanIterator[T any](ch <-chan T) *Iterator[T] {
var zero T
return New(func() (T, bool) {
v, ok := <-ch
if !ok {
return zero, false
}
return v, true
})
}
// NewMapIterator creates iterator yielding key-value pairs from map.
// NewMapIterator uses reflect package to keep iteration state.
func NewMapIterator[K comparable, V any](m map[K]V) *Iterator[Pair[K, V]] {
mapIter := reflect.ValueOf(m).MapRange()
return New(func() (Pair[K, V], bool) {
if !mapIter.Next() {
return Pair[K, V]{}, false
}
return Pair[K, V]{
First: mapIter.Key().Interface().(K),
Second: mapIter.Value().Interface().(V),
}, true
})
}
// NewMapKeysIterator creates iterator yielding keys from map.
// NewMapKeysIterator uses reflect package to keep iteration state.
func NewMapKeysIterator[K comparable, V any](m map[K]V) *Iterator[K] {
mapIter := reflect.ValueOf(m).MapRange()
return New(func() (K, bool) {
if !mapIter.Next() {
var zero K
return zero, false
}
return mapIter.Key().Interface().(K), true
})
}
// NewMapValuesIterator creates iterator yielding values from map.
// NewMapValuesIterator uses reflect package to keep iteration state.
func NewMapValuesIterator[K comparable, V any](m map[K]V) *Iterator[V] {
mapIter := reflect.ValueOf(m).MapRange()
return New(func() (V, bool) {
if !mapIter.Next() {
var zero V
return zero, false
}
return mapIter.Value().Interface().(V), true
})
}
// NewAsciiIterator creates iterator yielding bytes from string
// (interpreting string as []byte).
func NewAsciiIterator(s string) *Iterator[byte] {
var (
idx int
zero byte
)
return New(func() (byte, bool) {
if idx >= len(s) {
return zero, false
}
v := s[idx]
idx++
return v, true
})
}
// NewUTF8Iterator creates iterator yielding runes from string
// (interpreting string as []rune)
func NewUTF8Iterator(s string) *Iterator[rune] {
return New(func() (rune, bool) {
r, size := utf8.DecodeRuneInString(s)
if r == utf8.RuneError {
return 0, false
}
s = s[size:]
return r, true
})
}