This repository has been archived by the owner on Aug 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
iter.go
431 lines (347 loc) · 9.78 KB
/
iter.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
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
/*
Package iter provides generic, lazy iterators, functions for producing them
from primitive types, as well as functions and methods for transforming
and consuming them.
When reading the documentation of the functions contained in this package, you
should assume that any function which accepts an iterator, but does not return
one, consumes it unless otherwise stated, meaning that the values contained
within cannot be used again.
It might seem as though this package's API contains some duplicate
functionality, since there are many functions that appear to do the same thing
as methods of the same names defined on Iter[T]. However, there is an important
difference between these functions and their method counterparts. Since Go
does not support type parameters on methods, (Iter[T]).Map can only return
Iter[T] (an iterator of the same type as the input iterator). Map does not have
this limitation; it can map from an Iter[T] to an Iter[U] (an iterator with a
different type than the input iterator). The method versions are still provided
even though their functionality is a strict subset of that of the function
versions because using methods when possible results in more readable code.
*/
package iter
import "sync"
// Iter is a generic iterator function, the basis of this whole package. Note
// that the typical iter.Next() method is replaced with iter(), since Iter is
// simply defined as func() (T, bool).
type Iter[T any] func() (T, bool)
// Consume fetches the next value of the iterator until no more values are
// found. Note that it doesn't do anything with the values that are produced,
// but it can be useful in certain cases, such dropping a fixed number of items
// from an iterator by chaining Take and Consume.
func (i Iter[T]) Consume() {
for {
_, ok := i()
if !ok {
break
}
}
}
// Collect fetches the next value of the iterator until no more values are
// found, places these values in a slice, and returns it. Note that since
// Collect does not know how many values it will find, it must resize the
// slice multiple times during the collection process. This can result in poor
// performance, so CollectInto should be used when possible.
func (i Iter[T]) Collect() []T {
res := []T{}
for {
next, ok := i()
if !ok {
break
}
res = append(res, next)
}
return res
}
// CollectInto inserts values yielded by the input iterator into the provided
// slice, and returns the number of values it was able to add before the
// iterator was exhausted or the slice was full.
func (i Iter[T]) CollectInto(buf []T) int {
for j := 0; j < len(buf); j++ {
next, ok := i()
if !ok {
return j
}
buf[j] = next
}
return len(buf)
}
// All returns whether all values of the iterator satisfy the provided
// predicate function.
func (i Iter[T]) All(f func(T) bool) bool {
for {
next, ok := i()
if !ok {
break
}
if !f(next) {
return false
}
}
return true
}
// Any returns whether any of the values of the iterator satisfy the provided
// predicate function.
func (i Iter[T]) Any(f func(T) bool) bool {
for {
next, ok := i()
if !ok {
break
}
if f(next) {
return true
}
}
return false
}
// Count returns the number of remaining values in the iterator.
func (i Iter[T]) Count() int {
j := 0
for {
_, ok := i()
if !ok {
break
}
j++
}
return j
}
// Find returns the first value in the iterator that satisfies the provided
// predicate function, as well as a boolean indicating whether any value was
// found. It consumes all values up to the first satisfactory value, or the
// whole iterator if no values satisfy the predicate.
func (i Iter[T]) Find(f func(T) bool) (T, bool) {
for {
next, ok := i()
if !ok {
break
}
if f(next) {
return next, true
}
}
var z T
return z, false
}
// FindMap returns the first transformed value in the iterator for which the
// provided function does not return an error. As with Find, it consumes all
// values up to the first passing one.
func (i Iter[T]) FindMap(f func(T) (T, error)) (T, bool) {
for {
next, ok := i()
if !ok {
break
}
if mappedNext, err := f(next); err == nil {
return mappedNext, true
}
}
var z T
return z, false
}
// FindMap returns the first transformed value in the iterator for which the
// provided function does not return an error. As with Find, it consumes all
// values up to the first passing one.
func FindMap[T, U any](i Iter[T], f func(T) (U, error)) (U, bool) {
for {
next, ok := i()
if !ok {
break
}
if mappedNext, err := f(next); err == nil {
return mappedNext, true
}
}
var z U
return z, false
}
// Fold repeatedly applies the provided function to the current value (starting
// with init) and the next value of the iterator, until the whole iterator
// is consumed.
func (i Iter[T]) Fold(init T, f func(curr T, next T) T) T {
curr := init
for {
next, ok := i()
if !ok {
break
}
curr = f(curr, next)
}
return curr
}
// Fold repeatedly applies the provided function to the current value (starting
// with init) and the next value of the iterator, until the whole iterator
// is consumed.
func Fold[T, U any](i Iter[T], init U, f func(curr U, next T) U) U {
curr := init
for {
next, ok := i()
if !ok {
break
}
curr = f(curr, next)
}
return curr
}
// ForEach applies the provided function to all remaining values in the current
// iterator.
func (i Iter[T]) ForEach(f func(T)) {
for {
next, ok := i()
if !ok {
break
}
f(next)
}
}
// ForEachParallel applies the provided function to all remaining values
// in the current iterator. It differs from ForEach in that, where ForEach
// runs on a single thread and waits for each execution of the function to
// complete before fetching the next value and calling the function again,
// ForEachParallel performs executions of the function on different threads and
// only waits for all executions at the end. When the function to be executed
// is expensive and the order in which values of the iterator are operated upon
// does not matter, this method can result in better performance than ForEach.
func (i Iter[T]) ForEachParallel(f func(T)) {
var wg sync.WaitGroup
for {
next, ok := i()
if !ok {
break
}
wg.Add(1)
go func() {
defer wg.Done()
f(next)
}()
}
wg.Wait()
}
// Last returns the final value of the iterator, along with a boolean
// indicating whether the operation was successful, in other words, whether the
// input iterator was non-empty.
func (i Iter[T]) Last() (T, bool) {
curr, ok := i()
if !ok {
var z T
return z, false
}
for {
next, ok := i()
if ok {
curr = next
} else {
return curr, true
}
}
}
// Nth returns the nth value in the iterator, and a boolean indicating whether
// the iterator was too short. The provided value of n should be non-negative.
func (i Iter[T]) Nth(n int) (T, bool) {
for j := 0; j < n; j++ {
_, ok := i()
if !ok {
var z T
return z, false
}
}
res, ok := i()
if ok {
return res, true
}
var z T
return z, false
}
// TryFold applies the provided fallible function to the current value
// (starting with init) and the next value of the iterator, until the whole
// iterator is consumed. If at any point an error is returned, the operation
// stops and that error is returned.
func (i Iter[T]) TryFold(init T, f func(curr T, next T) (T, error)) (T, error) {
curr := init
for {
next, ok := i()
if !ok {
break
}
var err error
curr, err = f(curr, next)
if err != nil {
var z T
return z, err
}
}
return curr, nil
}
// TryFold applies the provided fallible function to the current value
// (starting with init) and the next value of the iterator, until the whole
// iterator is consumed. If at any point an error is returned, the operation
// stops and that error is returned.
func TryFold[T, U any](i Iter[T], init U, f func(curr U, next T) (U, error)) (U, error) {
curr := init
for {
next, ok := i()
if !ok {
break
}
var err error
curr, err = f(curr, next)
if err != nil {
var z U
return z, err
}
}
return curr, nil
}
// TryForEach applies the provided fallible function to all remaining values in
// the current iterator, but stops if an error is returned at any point.
func (i Iter[T]) TryForEach(f func(T) error) error {
for {
next, ok := i()
if !ok {
break
}
err := f(next)
if err != nil {
return err
}
}
return nil
}
// Reduce repeatedly applies the provided function to the current value
// (starting with iterator's first value) and the next value of the iterator,
// until the whole iterator is consumed. The second return value is true if the
// input iterator was non-empty, in which case the first return value is the
// result of the final application of f (or just the first element of the input
// if there was only one element). Otherwise, if the input iterator is empty,
// the second return value is false and the first is the zero value.
func (i Iter[T]) Reduce(f func(curr T, next T) T) (T, bool) {
curr, ok := i()
if !ok {
var z T
return z, false
}
return i.Fold(curr, f), true
}
// Pos returns the index of the first element satisfying the provided function,
// or -1 if one is not found. It consumes every element up to and including
// the element that satisfies the function, or the whole iterator if no no
// satisfactory element is found.
func (i Iter[T]) Pos(f func(T) bool) int {
for j := 0; ; j++ {
next, ok := i()
if !ok {
return -1
}
if f(next) {
return j
}
}
}
// Rev produces a new iterator whose values are reversed from those of the
// input iterator. Note that this method is not lazy, it must consume the whole
// input iterator immediately to produce the reversed result.
func (i Iter[T]) Rev() Iter[T] {
collected := i.Collect()
for j, k := 0, len(collected)-1; j < k; j, k = j+1, k-1 {
collected[j], collected[k] = collected[k], collected[j]
}
return Elems(collected)
}