-
Notifications
You must be signed in to change notification settings - Fork 0
/
itertools.go
411 lines (385 loc) · 9.15 KB
/
itertools.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
// Package itertools provides operators for iterators.
//
// For functions that consume or create operators please check the [from] and [to]
// packages in this module.
//
// # Guarantees
//
// All operators are guaranteed to:
// - run in linear time
// - allocate constant memory
// - depend only on the iter and constraints packages
// - not spawn additional goroutines
//
// Operators that cannot be implemented within these constraint will be added to
// a separate packages.
//
// # Idiomatic Use
//
// Go tends to be a very clear language that favors readability over compact code.
// All users of this library should try their best to preserve this property when
// manipulating iterators.
//
// The suggested way to do so is to name intermediate iterators when a manipulation
// chain is implemented instead of nesting calls.
//
// Example:
//
// numbersTo4 := slices.Values([]int{1,2,3,4})
// odds := Filter(numbers, func(i int) bool {
// return i%2 != 0
// })
// doubled := Map(odds, func(i int) int {
// return i*2
// })
// result := slices.Collect(doubled)
//
// Is preferable to a call chain like:
//
// result := slices.Collect(Map(Filter(slices.Values([]int{...
//
// If the same combination of operators is used more than twice it's strongly advised
// to create helper functions with telling names.
//
// [from]: https://pkg.go.dev/github.com/empijei/itertools/from
// [to]: https://pkg.go.dev/github.com/empijei/itertools/to
package itertools
import "iter"
/***********
* Cropping *
************/
// TakeN emits the first n items of the source iterator.
// This can be seen as a slice operation such as myIterator[:n+1].
func TakeN[T any](src iter.Seq[T], n int) iter.Seq[T] {
return func(yield func(T) bool) {
next, stop := iter.Pull(src)
defer stop()
for i := 0; i < n; i++ {
t, ok := next()
if !ok {
return
}
if !yield(t) {
return
}
}
}
}
// TakeWhile mirrors the source iterator while predicate returns true, and stops at the first false.
func TakeWhile[T any](src iter.Seq[T], predicate func(T) (ok bool)) iter.Seq[T] {
return func(yield func(T) bool) {
for t := range src {
if !predicate(t) {
return
}
if !yield(t) {
return
}
}
}
}
// SkipN discards the first n items of the source iterator and forwards the remaining items.
// This can be seen as a slice operation such as myIterator[n:].
func SkipN[T any](src iter.Seq[T], n int) iter.Seq[T] {
return func(yield func(T) bool) {
next, stop := iter.Pull(src)
defer stop()
for range n {
_, ok := next()
if !ok {
return
}
}
for {
t, ok := next()
if !ok {
return
}
if !yield(t) {
return
}
}
}
}
// SkipUntil discards all values until predicate returns true for the first time.
// Then it stops calling predicate and forwards the first accepted value and all the remaining ones.
func SkipUntil[T any](src iter.Seq[T], predicate func(T) (ok bool)) iter.Seq[T] {
return func(yield func(T) bool) {
next, stop := iter.Pull(src)
defer stop()
for {
v, ok := next()
if !ok {
return
}
if predicate(v) {
if !yield(v) {
return
}
break
}
}
for {
v, ok := next()
if !ok {
return
}
if !yield(v) {
return
}
}
}
}
/***********************
* Plucking and packing *
************************/
// Keys emits the keys, or first items of every couple emitted by the source iterator.
func Keys[K, V any](src iter.Seq2[K, V]) iter.Seq[K] {
return func(yield func(K) bool) {
for k := range src {
if !yield(k) {
return
}
}
}
}
// Values emits the values, or second items of every couple emitted by the source iterator.
func Values[K, V any](src iter.Seq2[K, V]) iter.Seq[V] {
return func(yield func(V) bool) {
for _, v := range src {
if !yield(v) {
return
}
}
}
}
// Entries emits couples of values that represent the key-value pairs from the source iterator.
func Entries[K, V any](src iter.Seq2[K, V]) iter.Seq[struct {
K K
V V
}] {
return func(yield func(struct {
K K
V V
}) bool) {
for k, v := range src {
if !yield(struct {
K K
V V
}{k, v}) {
return
}
}
}
}
/***************
* Transforming *
****************/
// Map applies the predicate to the source iterator until either source is exhausted
// or the consumer stops the iteration.
func Map[T, V any](src iter.Seq[T], predicate func(T) V) iter.Seq[V] {
return func(yield func(V) bool) {
for t := range src {
if v := predicate(t); !yield(v) {
return
}
}
}
}
// Map2 is like [Map] for iter.Seq2.
func Map2[K1, V1, K2, V2 any](src iter.Seq2[K1, V1], predicate func(K1, V1) (K2, V2)) iter.Seq2[K2, V2] {
return func(yield func(K2, V2) bool) {
for k1, v1 := range src {
if k2, v2 := predicate(k1, v1); !yield(k2, v2) {
return
}
}
}
}
// Map12 is like [Map] but it transforms the iterator from Seq to Seq2.
func Map12[T, K, V any](src iter.Seq[T], predicate func(T) (K, V)) iter.Seq2[K, V] {
return func(yield func(K, V) bool) {
for t := range src {
if k, v := predicate(t); !yield(k, v) {
return
}
}
}
}
// Map21 is like [Map] but it transforms the iterator from Seq2 to Seq.
func Map21[K, V, T any](src iter.Seq2[K, V], predicate func(K, V) T) iter.Seq[T] {
return func(yield func(T) bool) {
for k, v := range src {
if t := predicate(k, v); !yield(t) {
return
}
}
}
}
// Filter emits the item that the predicate returns true for.
func Filter[T any](src iter.Seq[T], predicate func(T) (ok bool)) iter.Seq[T] {
return func(yield func(T) bool) {
for t := range src {
if ok := predicate(t); !ok {
continue
}
if !yield(t) {
return
}
}
}
}
// Filter2 is like [Filter] for Seq2.
func Filter2[K, V any](src iter.Seq2[K, V], predicate func(K, V) (ok bool)) iter.Seq2[K, V] {
return func(yield func(K, V) bool) {
for k, v := range src {
if ok := predicate(k, v); !ok {
continue
}
if !yield(k, v) {
return
}
}
}
}
// PairWise emits all values with the value that preceded them.
// This means all values will be emitted twice except for the first and last one.
// Values are emitted once as the second value, then as the first, in this order.
// Pairs can be imagined as a sliding window on the source iterator.
func PairWise[T any](src iter.Seq[T]) iter.Seq2[T, T] {
return func(yield func(T, T) bool) {
next, stop := iter.Pull(src)
defer stop()
prev, ok := next()
if !ok {
return
}
for {
cur, ok := next()
if !ok {
return
}
if !yield(prev, cur) {
return
}
prev = cur
}
}
}
// Zip emits every time both source iterators have emitted
// a value, thus generating couples of values where no source value is used more than
// once and no one is discarded except for the trailing ones after one of the sources
// has stopped generating values.
func Zip[T, V any](src1 iter.Seq[T], src2 iter.Seq[V]) iter.Seq2[T, V] {
return func(yield func(T, V) bool) {
next1, stop1 := iter.Pull(src1)
defer stop1()
next2, stop2 := iter.Pull(src2)
defer stop2()
for {
t, ok1 := next1()
if !ok1 {
return
}
v, ok2 := next2()
if !ok2 {
return
}
if !yield(t, v) {
return
}
}
}
}
// Tap calls peek for all values emitted by src and consumed by the returned Seq.
//
// Peek must not modify or keep a reference to the values it observes.
func Tap[T any](src iter.Seq[T], peek func(T)) iter.Seq[T] {
return func(yield func(T) bool) {
for t := range src {
peek(t)
if !yield(t) {
return
}
}
}
}
// Deduplicate removes duplicates emitted by src. It doesn't check that
// the entire iterator never emits two identical values, it just removes consecutive
// identical values.
func Deduplicate[T comparable](src iter.Seq[T]) iter.Seq[T] {
return func(yield func(T) bool) {
next, stop := iter.Pull(src)
defer stop()
prev, ok := next()
if !ok || !yield(prev) {
return
}
for {
t, ok := next()
if !ok {
return
}
if t == prev {
continue
}
prev = t
if !yield(t) {
return
}
}
}
}
/***************
* Higher order *
****************/
// Flatten emits all values emitted by the inner iterators, flattening the source iterator
// structure to be one layer.
func Flatten[T any](src iter.Seq[iter.Seq[T]]) iter.Seq[T] {
return func(yield func(T) bool) {
for i := range src {
for t := range i {
if !yield(t) {
return
}
}
}
}
}
// FlattenSlice is like [Flatten] for iterators of slices.
func FlattenSlice[T any](src iter.Seq[[]T]) iter.Seq[T] {
return func(yield func(T) bool) {
for i := range src {
for _, t := range i {
if !yield(t) {
return
}
}
}
}
}
// Flatten2 emits all values emitted by the inner iterators, flattening the source iterator
// structure to be one layer. Keys for inner iterators are repeated for every inner emission.
func Flatten2[K, V any](src iter.Seq2[K, iter.Seq[V]]) iter.Seq2[K, V] {
return func(yield func(K, V) bool) {
for k, i := range src {
for v := range i {
if !yield(k, v) {
return
}
}
}
}
}
// Concat emits all values from the provided sources, in order.
func Concat[T any](srcs ...iter.Seq[T]) iter.Seq[T] {
return func(yield func(T) bool) {
for _, src := range srcs {
for t := range src {
if !yield(t) {
return
}
}
}
}
}