itertools 1.7.0 → 2.0.0-beta1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/dist/index.d.mts +433 -0
- package/dist/index.d.ts +433 -0
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +2 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +42 -5
- package/CHANGELOG.md +0 -94
- package/builtins.d.ts +0 -18
- package/builtins.js +0 -467
- package/builtins.js.flow +0 -294
- package/custom.d.ts +0 -7
- package/custom.js +0 -186
- package/custom.js.flow +0 -82
- package/index.d.ts +0 -52
- package/index.js +0 -301
- package/index.js.flow +0 -56
- package/itertools.d.ts +0 -52
- package/itertools.js +0 -1287
- package/itertools.js.flow +0 -423
- package/more-itertools.d.ts +0 -13
- package/more-itertools.js +0 -739
- package/more-itertools.js.flow +0 -261
- package/types.d.ts +0 -3
- package/types.js +0 -1
- package/types.js.flow +0 -5
- package/utils.d.ts +0 -8
- package/utils.js +0 -48
- package/utils.js.flow +0 -42
package/itertools.js.flow
DELETED
|
@@ -1,423 +0,0 @@
|
|
|
1
|
-
// @flow strict
|
|
2
|
-
|
|
3
|
-
import { all, enumerate, iter, range } from './builtins';
|
|
4
|
-
import { flatten } from './more-itertools';
|
|
5
|
-
import type { Maybe, Predicate, Primitive } from './types';
|
|
6
|
-
import { primitiveIdentity } from './utils';
|
|
7
|
-
|
|
8
|
-
const SENTINEL = Symbol();
|
|
9
|
-
|
|
10
|
-
function composeAnd(f1: (number) => boolean, f2: (number) => boolean): (number) => boolean {
|
|
11
|
-
return (n: number) => f1(n) && f2(n);
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
function slicePredicate(start: number, stop: ?number, step: number) {
|
|
15
|
-
// If stop is not provided (= undefined), then interpret the start value as the stop value
|
|
16
|
-
let _start = start,
|
|
17
|
-
_stop = stop,
|
|
18
|
-
_step = step;
|
|
19
|
-
if (_stop === undefined) {
|
|
20
|
-
[_start, _stop] = [0, _start];
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
let pred = (n: number) => n >= _start;
|
|
24
|
-
|
|
25
|
-
if (_stop !== null) {
|
|
26
|
-
const stopNotNull = _stop;
|
|
27
|
-
pred = composeAnd(pred, (n: number) => n < stopNotNull);
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
if (_step > 1) {
|
|
31
|
-
pred = composeAnd(pred, (n: number) => (n - _start) % _step === 0);
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
return pred;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
/**
|
|
38
|
-
* Returns an iterator that returns elements from the first iterable until it
|
|
39
|
-
* is exhausted, then proceeds to the next iterable, until all of the iterables
|
|
40
|
-
* are exhausted. Used for treating consecutive sequences as a single
|
|
41
|
-
* sequence.
|
|
42
|
-
*/
|
|
43
|
-
export function chain<T>(...iterables: Array<Iterable<T>>): Iterable<T> {
|
|
44
|
-
return flatten(iterables);
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
/**
|
|
48
|
-
* Returns an iterator that counts up values starting with number `start`
|
|
49
|
-
* (default 0), incrementing by `step`. To decrement, use a negative step
|
|
50
|
-
* number.
|
|
51
|
-
*/
|
|
52
|
-
export function* count(start: number = 0, step: number = 1): Iterable<number> {
|
|
53
|
-
let n = start;
|
|
54
|
-
for (;;) {
|
|
55
|
-
yield n;
|
|
56
|
-
n += step;
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
/**
|
|
61
|
-
* Non-lazy version of icompress().
|
|
62
|
-
*/
|
|
63
|
-
export function compress<T>(data: Iterable<T>, selectors: Iterable<boolean>): Array<T> {
|
|
64
|
-
return Array.from(icompress(data, selectors));
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
/**
|
|
68
|
-
* Returns an iterator producing elements from the iterable and saving a copy
|
|
69
|
-
* of each. When the iterable is exhausted, return elements from the saved
|
|
70
|
-
* copy. Repeats indefinitely.
|
|
71
|
-
*/
|
|
72
|
-
export function* cycle<T>(iterable: Iterable<T>): Iterable<T> {
|
|
73
|
-
const saved = [];
|
|
74
|
-
for (const element of iterable) {
|
|
75
|
-
yield element;
|
|
76
|
-
saved.push(element);
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
while (saved.length > 0) {
|
|
80
|
-
for (const element of saved) {
|
|
81
|
-
yield element;
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
/**
|
|
87
|
-
* Returns an iterator that drops elements from the iterable as long as the
|
|
88
|
-
* predicate is true; afterwards, returns every remaining element. Note, the
|
|
89
|
-
* iterator does not produce any output until the predicate first becomes
|
|
90
|
-
* false.
|
|
91
|
-
*/
|
|
92
|
-
export function* dropwhile<T>(iterable: Iterable<T>, predicate: Predicate<T>): Iterable<T> {
|
|
93
|
-
const it = iter(iterable);
|
|
94
|
-
for (const value of it) {
|
|
95
|
-
if (!predicate(value)) {
|
|
96
|
-
yield value;
|
|
97
|
-
break;
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
for (const value of it) {
|
|
102
|
-
yield value;
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
export function* groupby<T>(
|
|
107
|
-
iterable: Iterable<T>,
|
|
108
|
-
keyFn: (T) => Primitive = primitiveIdentity
|
|
109
|
-
): Iterable<[Primitive, Iterable<T>]> {
|
|
110
|
-
const it = iter(iterable);
|
|
111
|
-
|
|
112
|
-
let currentValue;
|
|
113
|
-
// $FlowFixMe[incompatible-type] - deliberate use of the SENTINEL symbol
|
|
114
|
-
let currentKey: Primitive = SENTINEL;
|
|
115
|
-
let targetKey = currentKey;
|
|
116
|
-
|
|
117
|
-
const grouper = function* grouper(tgtKey) {
|
|
118
|
-
while (currentKey === tgtKey) {
|
|
119
|
-
yield currentValue;
|
|
120
|
-
|
|
121
|
-
const nextVal = it.next();
|
|
122
|
-
if (nextVal.done) return;
|
|
123
|
-
currentValue = nextVal.value;
|
|
124
|
-
currentKey = keyFn(currentValue);
|
|
125
|
-
}
|
|
126
|
-
};
|
|
127
|
-
|
|
128
|
-
for (;;) {
|
|
129
|
-
while (currentKey === targetKey) {
|
|
130
|
-
const nextVal = it.next();
|
|
131
|
-
if (nextVal.done) {
|
|
132
|
-
// $FlowFixMe[incompatible-type] - deliberate use of the SENTINEL symbol
|
|
133
|
-
currentKey = SENTINEL;
|
|
134
|
-
return;
|
|
135
|
-
}
|
|
136
|
-
currentValue = nextVal.value;
|
|
137
|
-
currentKey = keyFn(currentValue);
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
targetKey = currentKey;
|
|
141
|
-
yield [currentKey, grouper(targetKey)];
|
|
142
|
-
}
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
/**
|
|
146
|
-
* Returns an iterator that filters elements from data returning only those
|
|
147
|
-
* that have a corresponding element in selectors that evaluates to `true`.
|
|
148
|
-
* Stops when either the data or selectors iterables has been exhausted.
|
|
149
|
-
*/
|
|
150
|
-
export function* icompress<T>(data: Iterable<T>, selectors: Iterable<boolean>): Iterable<T> {
|
|
151
|
-
for (let [d, s] of izip(data, selectors)) {
|
|
152
|
-
if (s) {
|
|
153
|
-
yield d;
|
|
154
|
-
}
|
|
155
|
-
}
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
/**
|
|
159
|
-
* Returns an iterator that filters elements from iterable returning only those
|
|
160
|
-
* for which the predicate is true.
|
|
161
|
-
*/
|
|
162
|
-
export function* ifilter<T>(iterable: Iterable<T>, predicate: Predicate<T>): Iterable<T> {
|
|
163
|
-
for (let value of iterable) {
|
|
164
|
-
if (predicate(value)) {
|
|
165
|
-
yield value;
|
|
166
|
-
}
|
|
167
|
-
}
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
/**
|
|
171
|
-
* Returns an iterator that computes the given mapper function using arguments
|
|
172
|
-
* from each of the iterables.
|
|
173
|
-
*/
|
|
174
|
-
export function* imap<T, V>(iterable: Iterable<T>, mapper: (T) => V): Iterable<V> {
|
|
175
|
-
for (let value of iterable) {
|
|
176
|
-
yield mapper(value);
|
|
177
|
-
}
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
/**
|
|
181
|
-
* Returns an iterator that returns selected elements from the iterable. If
|
|
182
|
-
* `start` is non-zero, then elements from the iterable are skipped until start
|
|
183
|
-
* is reached. Then, elements are returned by making steps of `step` (defaults
|
|
184
|
-
* to 1). If set to higher than 1, items will be skipped. If `stop` is
|
|
185
|
-
* provided, then iteration continues until the iterator reached that index,
|
|
186
|
-
* otherwise, the iterable will be fully exhausted. `islice()` does not
|
|
187
|
-
* support negative values for `start`, `stop`, or `step`.
|
|
188
|
-
*/
|
|
189
|
-
export function* islice<T>(iterable: Iterable<T>, start: number, stop: ?number, step: number = 1): Iterable<T> {
|
|
190
|
-
/* istanbul ignore if */
|
|
191
|
-
if (start < 0) throw new Error('start cannot be negative');
|
|
192
|
-
/* istanbul ignore if */
|
|
193
|
-
if (typeof stop === 'number' && stop < 0) throw new Error('stop cannot be negative');
|
|
194
|
-
/* istanbul ignore if */
|
|
195
|
-
if (step < 0) throw new Error('step cannot be negative');
|
|
196
|
-
|
|
197
|
-
const pred = slicePredicate(start, stop, step);
|
|
198
|
-
for (const [i, value] of enumerate(iterable)) {
|
|
199
|
-
if (pred(i)) {
|
|
200
|
-
yield value;
|
|
201
|
-
}
|
|
202
|
-
}
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
/**
|
|
206
|
-
* Returns an iterator that aggregates elements from each of the iterables.
|
|
207
|
-
* Used for lock-step iteration over several iterables at a time. When
|
|
208
|
-
* iterating over two iterables, use `izip2`. When iterating over three
|
|
209
|
-
* iterables, use `izip3`, etc. `izip` is an alias for `izip2`.
|
|
210
|
-
*/
|
|
211
|
-
export function* izip2<T1, T2>(xs: Iterable<T1>, ys: Iterable<T2>): Iterable<[T1, T2]> {
|
|
212
|
-
const ixs = iter(xs);
|
|
213
|
-
const iys = iter(ys);
|
|
214
|
-
for (;;) {
|
|
215
|
-
const x = ixs.next();
|
|
216
|
-
const y = iys.next();
|
|
217
|
-
if (!x.done && !y.done) {
|
|
218
|
-
yield [x.value, y.value];
|
|
219
|
-
} else {
|
|
220
|
-
// One of the iterables exhausted
|
|
221
|
-
return;
|
|
222
|
-
}
|
|
223
|
-
}
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
/**
|
|
227
|
-
* Like izip2, but for three input iterables.
|
|
228
|
-
*/
|
|
229
|
-
export function* izip3<T1, T2, T3>(xs: Iterable<T1>, ys: Iterable<T2>, zs: Iterable<T3>): Iterable<[T1, T2, T3]> {
|
|
230
|
-
const ixs = iter(xs);
|
|
231
|
-
const iys = iter(ys);
|
|
232
|
-
const izs = iter(zs);
|
|
233
|
-
for (;;) {
|
|
234
|
-
const x = ixs.next();
|
|
235
|
-
const y = iys.next();
|
|
236
|
-
const z = izs.next();
|
|
237
|
-
if (!x.done && !y.done && !z.done) {
|
|
238
|
-
yield [x.value, y.value, z.value];
|
|
239
|
-
} else {
|
|
240
|
-
// One of the iterables exhausted
|
|
241
|
-
return;
|
|
242
|
-
}
|
|
243
|
-
}
|
|
244
|
-
}
|
|
245
|
-
|
|
246
|
-
export const izip = izip2;
|
|
247
|
-
|
|
248
|
-
/**
|
|
249
|
-
* Returns an iterator that aggregates elements from each of the iterables. If
|
|
250
|
-
* the iterables are of uneven length, missing values are filled-in with
|
|
251
|
-
* fillvalue. Iteration continues until the longest iterable is exhausted.
|
|
252
|
-
*/
|
|
253
|
-
export function* izipLongest2<T1, T2, D = void>(
|
|
254
|
-
xs: Iterable<T1>,
|
|
255
|
-
ys: Iterable<T2>,
|
|
256
|
-
filler: D
|
|
257
|
-
): Iterable<[T1 | D, T2 | D]> {
|
|
258
|
-
const ixs = iter(xs);
|
|
259
|
-
const iys = iter(ys);
|
|
260
|
-
for (;;) {
|
|
261
|
-
const x = ixs.next();
|
|
262
|
-
const y = iys.next();
|
|
263
|
-
if (x.done && y.done) {
|
|
264
|
-
// All iterables exhausted
|
|
265
|
-
return;
|
|
266
|
-
} else {
|
|
267
|
-
yield [!x.done ? x.value : filler, !y.done ? y.value : filler];
|
|
268
|
-
}
|
|
269
|
-
}
|
|
270
|
-
}
|
|
271
|
-
|
|
272
|
-
/**
|
|
273
|
-
* See izipLongest2, but for three.
|
|
274
|
-
*/
|
|
275
|
-
export function* izipLongest3<T1, T2, T3, D = void>(
|
|
276
|
-
xs: Iterable<T1>,
|
|
277
|
-
ys: Iterable<T2>,
|
|
278
|
-
zs: Iterable<T3>,
|
|
279
|
-
filler: D
|
|
280
|
-
): Iterable<[T1 | D, T2 | D, T3 | D]> {
|
|
281
|
-
const ixs = iter(xs);
|
|
282
|
-
const iys = iter(ys);
|
|
283
|
-
const izs = iter(zs);
|
|
284
|
-
for (;;) {
|
|
285
|
-
const x = ixs.next();
|
|
286
|
-
const y = iys.next();
|
|
287
|
-
const z = izs.next();
|
|
288
|
-
if (x.done && y.done && z.done) {
|
|
289
|
-
// All iterables exhausted
|
|
290
|
-
return;
|
|
291
|
-
} else {
|
|
292
|
-
yield [!x.done ? x.value : filler, !y.done ? y.value : filler, !z.done ? z.value : filler];
|
|
293
|
-
}
|
|
294
|
-
}
|
|
295
|
-
}
|
|
296
|
-
|
|
297
|
-
/**
|
|
298
|
-
* Like the other izips (`izip`, `izip3`, etc), but generalized to take an
|
|
299
|
-
* unlimited amount of input iterables. Think `izip(*iterables)` in Python.
|
|
300
|
-
*
|
|
301
|
-
* **Note:** Due to Flow type system limitations, you can only "generially" zip
|
|
302
|
-
* iterables with homogeneous types, so you cannot mix types like <A, B> like
|
|
303
|
-
* you can with izip2().
|
|
304
|
-
*/
|
|
305
|
-
export function* izipMany<T>(...iters: Array<Iterable<T>>): Iterable<Array<T>> {
|
|
306
|
-
// Make them all iterables
|
|
307
|
-
const iterables = iters.map(iter);
|
|
308
|
-
|
|
309
|
-
for (;;) {
|
|
310
|
-
const heads: Array<IteratorResult<T, void>> = iterables.map((xs) => xs.next());
|
|
311
|
-
if (all(heads, (h) => !h.done)) {
|
|
312
|
-
// $FlowFixMe[unclear-type]
|
|
313
|
-
yield heads.map((h) => ((h.value: any): T));
|
|
314
|
-
} else {
|
|
315
|
-
// One of the iterables exhausted
|
|
316
|
-
return;
|
|
317
|
-
}
|
|
318
|
-
}
|
|
319
|
-
}
|
|
320
|
-
|
|
321
|
-
/**
|
|
322
|
-
* Return successive `r`-length permutations of elements in the iterable.
|
|
323
|
-
*
|
|
324
|
-
* If `r` is not specified, then `r` defaults to the length of the iterable and
|
|
325
|
-
* all possible full-length permutations are generated.
|
|
326
|
-
*
|
|
327
|
-
* Permutations are emitted in lexicographic sort order. So, if the input
|
|
328
|
-
* iterable is sorted, the permutation tuples will be produced in sorted order.
|
|
329
|
-
*
|
|
330
|
-
* Elements are treated as unique based on their position, not on their value.
|
|
331
|
-
* So if the input elements are unique, there will be no repeat values in each
|
|
332
|
-
* permutation.
|
|
333
|
-
*/
|
|
334
|
-
export function* permutations<T>(iterable: Iterable<T>, r: Maybe<number>): Iterable<Array<T>> {
|
|
335
|
-
let pool = Array.from(iterable);
|
|
336
|
-
let n = pool.length;
|
|
337
|
-
let x = r === undefined ? n : r;
|
|
338
|
-
|
|
339
|
-
if (x > n) {
|
|
340
|
-
return;
|
|
341
|
-
}
|
|
342
|
-
|
|
343
|
-
let indices: Array<number> = Array.from(range(n));
|
|
344
|
-
let cycles: Array<number> = Array.from(range(n, n - x, -1));
|
|
345
|
-
let poolgetter = (i) => pool[i];
|
|
346
|
-
|
|
347
|
-
yield indices.slice(0, x).map(poolgetter);
|
|
348
|
-
|
|
349
|
-
while (n > 0) {
|
|
350
|
-
let cleanExit: boolean = true;
|
|
351
|
-
for (let i of range(x - 1, -1, -1)) {
|
|
352
|
-
cycles[i] -= 1;
|
|
353
|
-
if (cycles[i] === 0) {
|
|
354
|
-
indices = indices
|
|
355
|
-
.slice(0, i)
|
|
356
|
-
.concat(indices.slice(i + 1))
|
|
357
|
-
.concat(indices.slice(i, i + 1));
|
|
358
|
-
cycles[i] = n - i;
|
|
359
|
-
} else {
|
|
360
|
-
let j: number = cycles[i];
|
|
361
|
-
|
|
362
|
-
let [p, q] = [indices[indices.length - j], indices[i]];
|
|
363
|
-
indices[i] = p;
|
|
364
|
-
indices[indices.length - j] = q;
|
|
365
|
-
yield indices.slice(0, x).map(poolgetter);
|
|
366
|
-
cleanExit = false;
|
|
367
|
-
break;
|
|
368
|
-
}
|
|
369
|
-
}
|
|
370
|
-
|
|
371
|
-
if (cleanExit) {
|
|
372
|
-
return;
|
|
373
|
-
}
|
|
374
|
-
}
|
|
375
|
-
}
|
|
376
|
-
|
|
377
|
-
/**
|
|
378
|
-
* Returns an iterator that produces values over and over again. Runs
|
|
379
|
-
* indefinitely unless the times argument is specified.
|
|
380
|
-
*/
|
|
381
|
-
export function* repeat<T>(thing: T, times?: number): Iterable<T> {
|
|
382
|
-
if (times === undefined) {
|
|
383
|
-
for (;;) {
|
|
384
|
-
yield thing;
|
|
385
|
-
}
|
|
386
|
-
} else {
|
|
387
|
-
// eslint-disable-next-line no-unused-vars
|
|
388
|
-
for (const i of range(times)) {
|
|
389
|
-
yield thing;
|
|
390
|
-
}
|
|
391
|
-
}
|
|
392
|
-
}
|
|
393
|
-
|
|
394
|
-
/**
|
|
395
|
-
* Returns an iterator that produces elements from the iterable as long as the
|
|
396
|
-
* predicate is true.
|
|
397
|
-
*/
|
|
398
|
-
export function* takewhile<T>(iterable: Iterable<T>, predicate: Predicate<T>): Iterable<T> {
|
|
399
|
-
for (const value of iterable) {
|
|
400
|
-
if (!predicate(value)) return;
|
|
401
|
-
yield value;
|
|
402
|
-
}
|
|
403
|
-
}
|
|
404
|
-
|
|
405
|
-
export function zipLongest2<T1, T2, D = void>(xs: Iterable<T1>, ys: Iterable<T2>, filler: D): Array<[T1 | D, T2 | D]> {
|
|
406
|
-
return Array.from(izipLongest2(xs, ys, filler));
|
|
407
|
-
}
|
|
408
|
-
|
|
409
|
-
export function zipLongest3<T1, T2, T3, D = void>(
|
|
410
|
-
xs: Iterable<T1>,
|
|
411
|
-
ys: Iterable<T2>,
|
|
412
|
-
zs: Iterable<T3>,
|
|
413
|
-
filler: D
|
|
414
|
-
): Array<[T1 | D, T2 | D, T3 | D]> {
|
|
415
|
-
return Array.from(izipLongest3(xs, ys, zs, filler));
|
|
416
|
-
}
|
|
417
|
-
|
|
418
|
-
export const izipLongest = izipLongest2;
|
|
419
|
-
export const zipLongest = zipLongest2;
|
|
420
|
-
|
|
421
|
-
export function zipMany<T>(...iters: Array<Iterable<T>>): Array<Array<T>> {
|
|
422
|
-
return Array.from(izipMany(...iters));
|
|
423
|
-
}
|
package/more-itertools.d.ts
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import { Predicate, Primitive } from './types';
|
|
2
|
-
|
|
3
|
-
export function chunked<T>(iterable: Iterable<T>, size: number): Iterable<T[]>;
|
|
4
|
-
export function flatten<T>(iterableOfIterables: Iterable<Iterable<T>>): Iterable<T>;
|
|
5
|
-
export function intersperse<T>(value: T, iterable: Iterable<T>): Iterable<T>;
|
|
6
|
-
export function itake<T>(n: number, iterable: Iterable<T>): Iterable<T>;
|
|
7
|
-
export function pairwise<T>(iterable: Iterable<T>): Iterable<[T, T]>;
|
|
8
|
-
export function partition<T>(iterable: Iterable<T>, predicate: Predicate<T>): [T[], T[]];
|
|
9
|
-
export function roundrobin<T>(...iters: Array<Iterable<T>>): Iterable<T>;
|
|
10
|
-
export function heads<T>(...iters: Array<Iterable<T>>): Iterable<T[]>;
|
|
11
|
-
export function take<T>(n: number, iterable: Iterable<T>): T[];
|
|
12
|
-
export function uniqueEverseen<T>(iterable: Iterable<T>, keyFn?: (item: T) => Primitive): Iterable<T>;
|
|
13
|
-
export function uniqueJustseen<T>(iterable: Iterable<T>, keyFn?: (item: T) => Primitive): Iterable<T>;
|