-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathiterators.go
More file actions
292 lines (241 loc) · 6.42 KB
/
iterators.go
File metadata and controls
292 lines (241 loc) · 6.42 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
package iterator
import (
"context"
)
type emptyIterator[T any] struct{}
type constIterator[T any] struct {
value T
}
// Empty returns an empty iterator of type T
func Empty[T any]() Iterator[T] {
return &emptyIterator[T]{}
}
// Zero returns an infinite iterator with the zero value of type T
func Zero[T any]() Iterator[T] {
return Const(*new(T))
}
// Const returns an infinite iterator with with the given value
func Const[T any](value T) Iterator[T] {
return &constIterator[T]{value: value}
}
// Once returns an iterator with with the given value and size 1
func Once[T any](value T) Iterator[T] {
return Pipe(Const(value), Limit[T](1))
}
func (iter *constIterator[T]) Next() bool { return true }
func (iter *constIterator[T]) Get() (T, error) { return iter.value, nil }
func (iter *constIterator[T]) Close() error { return nil }
func (iter *constIterator[T]) Err() error { return nil }
func (iter *emptyIterator[T]) Next() bool { return false }
func (iter *emptyIterator[T]) Get() (T, error) { return *new(T), nil }
func (iter *emptyIterator[T]) Close() error { return nil }
func (iter *emptyIterator[T]) Err() error { return nil }
type sliceIterator[T any] struct {
source []T
curr int
}
// FromSlice returns an iterator wrapping a slice source
func FromSlice[T any](source []T) Iterator[T] {
return &sliceIterator[T]{source: source, curr: -1}
}
// ToSlice consumes all items in the iterator into a slice
func ToSlice[T any](iter Iterator[T]) ([]T, error) {
data := []T{}
_, err := Iterate(iter, func(_ int, item T) (bool, error) {
data = append(data, item)
return true, nil
})
if err != nil {
return nil, err
}
return data, nil
}
func (iter *sliceIterator[T]) Next() bool {
hasNext := iter.curr+1 < len(iter.source)
if hasNext {
iter.curr++
}
return hasNext
}
func (iter *sliceIterator[T]) Get() (T, error) { return iter.source[iter.curr], nil }
func (iter *sliceIterator[T]) Close() error { return nil }
func (iter *sliceIterator[T]) Err() error { return nil }
type (
channelIterator[T any] struct {
source <-chan T
value T
}
valErrChannelIterator[T any] struct {
source <-chan ValErr[T]
value T
err error
}
)
// FromChannel returns an iterator wrapping a channel source
func FromChannel[T any](source <-chan T) Iterator[T] {
return &channelIterator[T]{source: source}
}
// FromValErrChannel returns an iterator wrapping a channel source with items of type ValErr
func FromValErrChannel[T any](source <-chan ValErr[T]) Iterator[T] {
return &valErrChannelIterator[T]{source: source}
}
func (iter *channelIterator[T]) Next() bool {
iter.value = *new(T)
if iter.source == nil {
return false
}
value, ok := <-iter.source
if ok {
iter.value = value
} else {
iter.source = nil
}
return ok
}
func (iter *valErrChannelIterator[T]) Next() bool {
iter.value, iter.err = *new(T), nil
if iter.source == nil {
return false
}
value, ok := <-iter.source
if ok {
iter.value, iter.err = value.Val, value.Err
} else {
iter.source = nil
}
return ok
}
func (iter *channelIterator[T]) Get() (T, error) { return iter.value, nil }
func (iter *channelIterator[T]) Close() error { return nil }
func (iter *channelIterator[T]) Err() error { return nil }
func (iter *valErrChannelIterator[T]) Get() (T, error) { return iter.value, iter.err }
func (iter *valErrChannelIterator[T]) Close() error { return nil }
func (iter *valErrChannelIterator[T]) Err() error { return iter.err }
// ToChannel consumes all items in the iterator into a channel with size as capacity
func ToChannel[T any](iter Iterator[T], size int) (<-chan ValErr[T], func()) {
stream := make(chan ValErr[T], size)
cancel := make(chan struct{})
go func() {
_, err := Iterate(iter, func(_ int, item T) (bool, error) {
select {
case <-cancel:
close(stream)
return false, nil
case stream <- ValErr[T]{Val: item}:
return true, nil
}
})
if err != nil {
stream <- ValErr[T]{Err: err}
}
close(stream)
}()
return stream, func() { cancel <- struct{}{} }
}
// FromMap returns an iterator wrapping a map source
func FromMap[K comparable, V any](source map[K]V) Iterator[KV[K, V]] {
c := make(chan KV[K, V], len(source)/4)
ctx, cancel := context.WithCancel(context.Background())
go func() {
for k, v := range source {
select {
case <-ctx.Done():
close(c)
return
case c <- KV[K, V]{Key: k, Val: v}:
continue
}
}
close(c)
}()
return OnClose(FromChannel(c), func() error {
cancel()
return nil
})
}
// ToMap consumes all items in a KV iterator into a map
func ToMap[K comparable, V any](iter Iterator[KV[K, V]]) (map[K]V, error) {
out := make(map[K]V)
_, err := Iterate(iter, func(_ int, item KV[K, V]) (bool, error) {
out[item.Key] = item.Val
return true, nil
})
if err != nil {
return nil, err
}
return out, nil
}
// OnClose extends an iterator with a close callback
func OnClose[T any](iter Iterator[T], fn func() error) Iterator[T] {
var err error
var closed bool
return &iterator[T]{
next: iter.Next,
get: iter.Get,
err: func() error {
if err != nil {
return err
}
return iter.Err()
},
close: func() error {
if closed {
return err
}
defer func() {
closed = true
iter.Close()
}()
if err = fn(); err != nil {
return err
}
err = iter.Close()
return err
},
}
}
type funcIterator[T any] struct {
next func() (T, bool, error)
value T
err error
done bool
}
func (iter *funcIterator[T]) Next() bool {
if iter.done {
return false
}
var hasMore bool
iter.value, hasMore, iter.err = iter.next()
if !hasMore {
iter.done = true
}
return hasMore
}
func (iter *funcIterator[T]) Get() (T, error) { return iter.value, iter.err }
func (iter *funcIterator[T]) Err() error { return iter.err }
func (iter *funcIterator[T]) Close() error { return nil }
// FromFunc returns an iterator wrapping a func source
func FromFunc[T any](next func() (T, bool, error)) Iterator[T] {
return &funcIterator[T]{
next: next,
}
}
// ToFunc returns a function to consume all items in the iterator
func ToFunc[T any](iter Iterator[T]) func() (T, bool, error) {
return func() (T, bool, error) {
if iter.Next() {
item, err := iter.Get()
if err != nil {
return *new(T), false, err
}
return item, true, nil
}
if err := iter.Err(); err != nil {
return *new(T), false, err
}
if err := iter.Close(); err != nil {
return *new(T), false, err
}
return *new(T), false, nil
}
}