-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathitertools.go
More file actions
359 lines (336 loc) · 8.91 KB
/
itertools.go
File metadata and controls
359 lines (336 loc) · 8.91 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
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
package itertools
import (
"cmp"
"iter"
"golang.org/x/exp/constraints"
)
// NewIterator creates a new iterator from a variadic list of values.
//
// Example:
//
// iter := itertools.NewIterator(1, 2, 3, 4, 5)
// result := iter.Collect()
// // result is []int{1, 2, 3, 4, 5}
func NewIterator[V any](v ...V) *Iterator[V] {
return &Iterator[V]{
seq: func(yield func(V) bool) {
for _, v := range v {
if !yield(v) {
return
}
}
},
}
}
// Zip combines two iterators element-wise into a single iterator of pairs.
// Iteration stops when either iterator is exhausted.
//
// The resulting iterator yields struct values with First and Second fields.
//
// Example:
//
// iter1 := itertools.ToIter([]int{1, 2, 3})
// iter2 := itertools.ToIter([]string{"a", "b", "c"})
// zipped := itertools.Zip(iter1, iter2).Collect()
// // zipped is [{First: 1, Second: "a"}, {First: 2, Second: "b"}, {First: 3, Second: "c"}]
func Zip[A, B any](it1 *Iterator[A], it2 *Iterator[B]) *Iterator[struct {
First A
Second B
}] {
return &Iterator[struct {
First A
Second B
}]{
seq: func(yield func(struct {
First A
Second B
}) bool,
) {
pull1, stop1 := iter.Pull(it1.seq)
pull2, stop2 := iter.Pull(it2.seq)
defer stop1()
defer stop2()
for {
v1, ok1 := pull1()
v2, ok2 := pull2()
if !ok1 || !ok2 {
return
}
if !yield(struct {
First A
Second B
}{v1, v2}) {
return
}
}
},
}
}
// Zip2 combines two iterators element-wise into a single iterator of pairs.
// Unlike Zip, if one iterator is longer than the other, the shorter one is
// extended using the fill values.
//
// Example:
//
// iter1 := itertools.ToIter([]int{1, 2, 3})
// iter2 := itertools.ToIter([]string{"a"})
// fill := struct{ First int; Second string }{0, ""}
// zipped := itertools.Zip2(iter1, iter2, fill).Collect()
// // zipped is [{1, "a"}, {2, ""}, {3, ""}]
func Zip2[A, B any](it1 *Iterator[A], it2 *Iterator[B], fill struct {
First A
Second B
}) *Iterator[struct {
First A
Second B
}] {
return &Iterator[struct {
First A
Second B
}]{
seq: func(yield func(struct {
First A
Second B
}) bool,
) {
pull1, stop1 := iter.Pull(it1.seq)
pull2, stop2 := iter.Pull(it2.seq)
defer stop1()
defer stop2()
for {
v1, ok1 := pull1()
v2, ok2 := pull2()
if !ok1 && !ok2 {
return
}
// Use fill values when one iterator ends
result := struct {
First A
Second B
}{}
if ok1 {
result.First = v1
} else {
result.First = fill.First
}
if ok2 {
result.Second = v2
} else {
result.Second = fill.Second
}
if !yield(result) {
return
}
}
},
}
}
// Fold accumulates the elements of the iterator using a binary operation.
// Also known as reduce or aggregate in other languages.
//
// The transform function takes an accumulator and the next value,
// returning the new accumulator value.
//
// Example:
//
// iter := itertools.ToIter([]int{1, 2, 3, 4, 5})
// sum := itertools.Fold(iter, func(acc, v int) int { return acc + v }, 0)
// // sum is 15
func Fold[V any, T any](it *Iterator[V], transform func(T, V) T, initial T) T {
acc := initial
it.seq(func(v V) bool {
acc = transform(acc, v)
return true
})
return acc
}
// Sum adds all elements of the iterator after applying the transform function.
// The zero parameter specifies the additive identity for the result type.
//
// Example:
//
// iter := itertools.ToIter([]int{1, 2, 3, 4, 5})
// sum := itertools.Sum(iter, func(v int) int { return v }, 0)
// // sum is 15
//
// // Sum of squares
// iter2 := itertools.ToIter([]int{1, 2, 3})
// sumSquares := itertools.Sum(iter2, func(v int) int { return v * v }, 0)
// // sumSquares is 14
func Sum[V any, T cmp.Ordered](it *Iterator[V], transform func(V) T, zero T) T {
return Fold(it, func(acc T, v V) T { return acc + transform(v) }, zero)
}
// Productable is a constraint for types that support multiplication.
type Productable interface {
constraints.Integer | constraints.Float | constraints.Complex
}
// Product multiplies all elements of the iterator after applying the transform function.
// The one parameter specifies the multiplicative identity for the result type.
//
// Example:
//
// iter := itertools.ToIter([]int{1, 2, 3, 4, 5})
// product := itertools.Product(iter, func(v int) int { return v }, 1)
// // product is 120
func Product[V any, T Productable](it *Iterator[V], transform func(V) T, one T) T {
return Fold(it, func(acc T, v V) T { return acc * transform(v) }, one)
}
// ChunkSlice returns an iterator that yields slices of up to `size` elements.
// The last chunk may contain fewer elements if the total is not divisible by size.
//
// Each chunk is a separate slice - modifications won't affect the original data.
//
// Example:
//
// iter := itertools.ToIter([]int{1, 2, 3, 4, 5, 6, 7})
// chunks := itertools.ChunkSlice(iter, 3).Collect()
// // chunks is [][]int{{1, 2, 3}, {4, 5, 6}, {7}}
func ChunkSlice[V any](it *Iterator[V], size int) *Iterator[[]V] {
return &Iterator[[]V]{
seq: func(yield func([]V) bool) {
chunk := make([]V, 0, size)
it.seq(func(v V) bool {
chunk = append(chunk, v)
if len(chunk) == size {
// Create a new slice to avoid sharing the underlying array
result := make([]V, size)
copy(result, chunk)
if !yield(result) {
return false
}
chunk = chunk[:0] // Clear the chunk while preserving capacity
}
return true
})
// Handle any remaining elements in the last chunk
if len(chunk) > 0 {
result := make([]V, len(chunk))
copy(result, chunk)
yield(result)
}
},
}
}
// Chunks returns an iterator that yields iterators, each containing up to `size` elements.
// The last chunk may contain fewer elements if the total is not divisible by size.
//
// Unlike ChunkSlice, this returns iterators instead of slices.
//
// Example:
//
// iter := itertools.ToIter([]int{1, 2, 3, 4, 5})
// chunks := itertools.Chunks(iter, 2)
// for chunks.Next() {
// chunk := chunks.Current()
// fmt.Println(chunk.Collect())
// }
// // Output: [1 2]
// // [3 4]
// // [5]
func Chunks[V any](it *Iterator[V], size int) *Iterator[*Iterator[V]] {
return &Iterator[*Iterator[V]]{
seq: func(yield func(*Iterator[V]) bool) {
chunk := make([]V, 0, size)
it.seq(func(v V) bool {
chunk = append(chunk, v)
if len(chunk) == size {
// Create a new slice to avoid sharing the underlying array
result := make([]V, size)
copy(result, chunk)
if !yield(ToIter(result)) {
return false
}
chunk = chunk[:0] // Clear the chunk while preserving capacity
}
return true
})
// Handle any remaining elements in the last chunk
if len(chunk) > 0 {
result := make([]V, len(chunk))
copy(result, chunk)
yield(ToIter(result))
}
},
}
}
// ChunkList returns a slice of iterators, each containing up to `size` elements.
// This is a convenience function that collects Chunks into a slice.
//
// Example:
//
// iter := itertools.ToIter([]int{1, 2, 3, 4, 5, 6})
// chunks := itertools.ChunkList(iter, 2)
// // chunks is []*Iterator with 3 iterators containing [1,2], [3,4], [5,6]
func ChunkList[V any](it *Iterator[V], size int) []*Iterator[V] {
return Chunks(it, size).Collect()
}
// Flatten concatenates multiple iterators into a single iterator.
// Elements are yielded in order: all elements from the first iterator,
// then all from the second, and so on.
//
// Properly handles early termination - stops immediately when yield returns false.
//
// Example:
//
// iter1 := itertools.ToIter([]int{1, 2, 3})
// iter2 := itertools.ToIter([]int{4, 5, 6})
// iter3 := itertools.ToIter([]int{7, 8, 9})
// flattened := itertools.Flatten(iter1, iter2, iter3).Collect()
// // flattened is []int{1, 2, 3, 4, 5, 6, 7, 8, 9}
func Flatten[V any](its ...*Iterator[V]) *Iterator[V] {
return &Iterator[V]{
seq: func(yield func(V) bool) {
for _, it := range its {
shouldContinue := true
it.seq(func(v V) bool {
if !yield(v) {
shouldContinue = false
return false
}
return true
})
if !shouldContinue {
return
}
}
},
}
}
// CartesianProduct returns an iterator of all pairs of elements from two iterators.
// Note: The second iterator (it2) is fully collected into memory to enable
// multiple iterations over it for each element in it1. Use with caution for large datasets.
//
// Example:
//
// iter1 := ToIter([]int{1, 2})
// iter2 := ToIter([]string{"a", "b"})
// product := CartesianProduct(iter1, iter2).Collect()
// // Result: [{1, "a"}, {1, "b"}, {2, "a"}, {2, "b"}]
func CartesianProduct[A, B any](it1 *Iterator[A], it2 *Iterator[B]) *Iterator[struct {
X A
Y B
}] {
xs := it2.Collect()
return &Iterator[struct {
X A
Y B
}]{
seq: func(yield func(struct {
X A
Y B
}) bool,
) {
it1.seq(func(a A) bool {
for _, b := range xs {
if !yield(struct {
X A
Y B
}{X: a, Y: b}) {
return false
}
}
return true
})
},
}
}