itertools 1.7.1 → 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 CHANGED
@@ -378,7 +378,7 @@ Returns an iterator that computes the given mapper function using arguments
378
378
  from each of the iterables.
379
379
 
380
380
 
381
- <a name="islice" href="#islice">#</a> <b>islice</b>(iterable: <i>Iterable&lt;T&gt;</i>[, start: <i>number</i>], stop: <i>number</i>[, step: <i>number</i>]): <i>Iterable&lt;T&gt;</i> [&lt;&gt;](https://github.com/nvie/itertools.js/blob/master/src/itertools.js "Source")
381
+ <a name="islice" href="#islice">#</a> <b>islice</b>(iterable: <i>Iterable&lt;T&gt;</i>[start: <i>number</i>], stop: <i>number</i>[, step: <i>number</i>]): <i>Iterable&lt;T&gt;</i> [&lt;&gt;](https://github.com/nvie/itertools.js/blob/master/src/itertools.js "Source")
382
382
 
383
383
  Returns an iterator that returns selected elements from the iterable. If
384
384
  `start` is non-zero, then elements from the iterable are skipped until start is
@@ -0,0 +1,433 @@
1
+ declare type Predicate<T> = (value: T) => boolean;
2
+ declare type Primitive = string | number | boolean;
3
+
4
+ /**
5
+ * Returns true when all of the items in iterable are truthy. An optional key
6
+ * function can be used to define what truthiness means for this specific
7
+ * collection.
8
+ *
9
+ * Examples:
10
+ *
11
+ * all([]) // => true
12
+ * all([0]) // => false
13
+ * all([0, 1, 2]) // => false
14
+ * all([1, 2, 3]) // => true
15
+ *
16
+ * Examples with using a key function:
17
+ *
18
+ * all([2, 4, 6], n => n % 2 === 0) // => true
19
+ * all([2, 4, 5], n => n % 2 === 0) // => false
20
+ *
21
+ */
22
+ declare function all<T>(iterable: Iterable<T>, keyFn?: Predicate<T>): boolean;
23
+ /**
24
+ * Returns true when any of the items in iterable are truthy. An optional key
25
+ * function can be used to define what truthiness means for this specific
26
+ * collection.
27
+ *
28
+ * Examples:
29
+ *
30
+ * any([]) // => false
31
+ * any([0]) // => false
32
+ * any([0, 1, null, undefined]) // => true
33
+ *
34
+ * Examples with using a key function:
35
+ *
36
+ * any([1, 4, 5], n => n % 2 === 0) // => true
37
+ * any([{name: 'Bob'}, {name: 'Alice'}], person => person.name.startsWith('C')) // => false
38
+ *
39
+ */
40
+ declare function any<T>(iterable: Iterable<T>, keyFn?: Predicate<T>): boolean;
41
+ /**
42
+ * Returns true when any of the items in the iterable are equal to the target object.
43
+ *
44
+ * Examples:
45
+ *
46
+ * contains([], 'whatever') // => false
47
+ * contains([3], 42) // => false
48
+ * contains([3], 3) // => true
49
+ * contains([0, 1, 2], 2) // => true
50
+ *
51
+ */
52
+ declare function contains<T>(haystack: Iterable<T>, needle: T): boolean;
53
+ /**
54
+ * Returns an iterable of enumeration pairs. Iterable must be a sequence, an
55
+ * iterator, or some other object which supports iteration. The elements
56
+ * produced by returns a tuple containing a counter value (starting from 0 by
57
+ * default) and the values obtained from iterating over given iterable.
58
+ *
59
+ * Example:
60
+ *
61
+ * import { enumerate } from 'itertools';
62
+ *
63
+ * console.log([...enumerate(['hello', 'world'])]);
64
+ * // [0, 'hello'], [1, 'world']]
65
+ */
66
+ declare function enumerate<T>(iterable: Iterable<T>, start?: number): Iterable<[number, T]>;
67
+ /**
68
+ * Non-lazy version of ifilter().
69
+ */
70
+ declare function filter<T>(iterable: Iterable<T>, predicate: Predicate<T>): T[];
71
+ /**
72
+ * Returns an iterator object for the given iterable. This can be used to
73
+ * manually get an iterator for any iterable datastructure. The purpose and
74
+ * main use case of this function is to get a single iterator (a thing with
75
+ * state, think of it as a "cursor") which can only be consumed once.
76
+ */
77
+ declare function iter<T>(iterable: Iterable<T>): IterableIterator<T>;
78
+ /**
79
+ * Non-lazy version of imap().
80
+ */
81
+ declare function map<T, V>(iterable: Iterable<T>, mapper: (item: T) => V): V[];
82
+ /**
83
+ * Return the largest item in an iterable. Only works for numbers, as ordering
84
+ * is pretty poorly defined on any other data type in JS. The optional `keyFn`
85
+ * argument specifies a one-argument ordering function like that used for
86
+ * sorted().
87
+ *
88
+ * If the iterable is empty, `undefined` is returned.
89
+ *
90
+ * If multiple items are maximal, the function returns either one of them, but
91
+ * which one is not defined.
92
+ */
93
+ declare function max<T>(iterable: Iterable<T>, keyFn?: (item: T) => number): T | undefined;
94
+ /**
95
+ * Return the smallest item in an iterable. Only works for numbers, as
96
+ * ordering is pretty poorly defined on any other data type in JS. The
97
+ * optional `keyFn` argument specifies a one-argument ordering function like
98
+ * that used for sorted().
99
+ *
100
+ * If the iterable is empty, `undefined` is returned.
101
+ *
102
+ * If multiple items are minimal, the function returns either one of them, but
103
+ * which one is not defined.
104
+ */
105
+ declare function min<T>(iterable: Iterable<T>, keyFn?: (item: T) => number): T | undefined;
106
+ /**
107
+ * Returns an iterator producing all the numbers in the given range one by one,
108
+ * starting from `start` (default 0), as long as `i < stop`, in increments of
109
+ * `step` (default 1).
110
+ *
111
+ * `range(a)` is a convenient shorthand for `range(0, a)`.
112
+ *
113
+ * Various valid invocations:
114
+ *
115
+ * range(5) // [0, 1, 2, 3, 4]
116
+ * range(2, 5) // [2, 3, 4]
117
+ * range(0, 5, 2) // [0, 2, 4]
118
+ * range(5, 0, -1) // [5, 4, 3, 2, 1]
119
+ * range(-3) // []
120
+ *
121
+ * For a positive `step`, the iterator will keep producing values `n` as long
122
+ * as the stop condition `n < stop` is satisfied.
123
+ *
124
+ * For a negative `step`, the iterator will keep producing values `n` as long
125
+ * as the stop condition `n > stop` is satisfied.
126
+ *
127
+ * The produced range will be empty if the first value to produce already does
128
+ * not meet the value constraint.
129
+ */
130
+ declare function range(a: number, ...rest: number[]): Iterable<number>;
131
+ /**
132
+ * Apply function of two arguments cumulatively to the items of sequence, from
133
+ * left to right, so as to reduce the sequence to a single value. For example:
134
+ *
135
+ * reduce([1, 2, 3, 4, 5], (x, y) => x + y, 0)
136
+ *
137
+ * calculates
138
+ *
139
+ * (((((0+1)+2)+3)+4)+5)
140
+ *
141
+ * The left argument, `x`, is the accumulated value and the right argument,
142
+ * `y`, is the update value from the sequence.
143
+ *
144
+ * **Difference between `reduce()` and `reduce\_()`**: `reduce()` requires an
145
+ * explicit initializer, whereas `reduce_()` will automatically use the first
146
+ * item in the given iterable as the initializer. When using `reduce()`, the
147
+ * initializer value is placed before the items of the sequence in the
148
+ * calculation, and serves as a default when the sequence is empty. When using
149
+ * `reduce_()`, and the given iterable is empty, then no default value can be
150
+ * derived and `undefined` will be returned.
151
+ */
152
+ declare function reduce<T, O>(iterable: Iterable<T>, reducer: (agg: O, item: T, index: number) => O, start: O): O;
153
+ /**
154
+ * Return a new sorted list from the items in iterable.
155
+ *
156
+ * Has two optional arguments:
157
+ *
158
+ * * `keyFn` specifies a function of one argument providing a primitive
159
+ * identity for each element in the iterable. that will be used to compare.
160
+ * The default value is to use a default identity function that is only
161
+ * defined for primitive types.
162
+ *
163
+ * * `reverse` is a boolean value. If `true`, then the list elements are
164
+ * sorted as if each comparison were reversed.
165
+ */
166
+ declare function sorted<T>(iterable: Iterable<T>, keyFn?: (item: T) => Primitive, reverse?: boolean): T[];
167
+ /**
168
+ * Sums the items of an iterable from left to right and returns the total. The
169
+ * sum will defaults to 0 if the iterable is empty.
170
+ */
171
+ declare function sum(iterable: Iterable<number>): number;
172
+ /**
173
+ * See izip.
174
+ */
175
+ declare function zip<T1, T2>(xs: Iterable<T1>, ys: Iterable<T2>): Array<[T1, T2]>;
176
+ /**
177
+ * See izip3.
178
+ */
179
+ declare function zip3<T1, T2, T3>(xs: Iterable<T1>, ys: Iterable<T2>, zs: Iterable<T3>): Array<[T1, T2, T3]>;
180
+
181
+ /**
182
+ * Returns an iterator that returns elements from the first iterable until it
183
+ * is exhausted, then proceeds to the next iterable, until all of the iterables
184
+ * are exhausted. Used for treating consecutive sequences as a single
185
+ * sequence.
186
+ */
187
+ declare function chain<T>(...iterables: Iterable<T>[]): Iterable<T>;
188
+ /**
189
+ * Returns an iterator that counts up values starting with number `start`
190
+ * (default 0), incrementing by `step`. To decrement, use a negative step
191
+ * number.
192
+ */
193
+ declare function count(start?: number, step?: number): Iterable<number>;
194
+ /**
195
+ * Non-lazy version of icompress().
196
+ */
197
+ declare function compress<T>(data: Iterable<T>, selectors: Iterable<boolean>): T[];
198
+ /**
199
+ * Returns an iterator producing elements from the iterable and saving a copy
200
+ * of each. When the iterable is exhausted, return elements from the saved
201
+ * copy. Repeats indefinitely.
202
+ */
203
+ declare function cycle<T>(iterable: Iterable<T>): Iterable<T>;
204
+ /**
205
+ * Returns an iterator that drops elements from the iterable as long as the
206
+ * predicate is true; afterwards, returns every remaining element. Note, the
207
+ * iterator does not produce any output until the predicate first becomes
208
+ * false.
209
+ */
210
+ declare function dropwhile<T>(iterable: Iterable<T>, predicate: Predicate<T>): Iterable<T>;
211
+ declare function groupby<T, K extends Primitive>(iterable: Iterable<T>, keyFn?: (item: T) => K): Generator<[K, Generator<T, undefined>], undefined>;
212
+ /**
213
+ * Returns an iterator that filters elements from data returning only those
214
+ * that have a corresponding element in selectors that evaluates to `true`.
215
+ * Stops when either the data or selectors iterables has been exhausted.
216
+ */
217
+ declare function icompress<T>(data: Iterable<T>, selectors: Iterable<boolean>): Iterable<T>;
218
+ /**
219
+ * Returns an iterator that filters elements from iterable returning only those
220
+ * for which the predicate is true.
221
+ */
222
+ declare function ifilter<T>(iterable: Iterable<T>, predicate: Predicate<T>): Iterable<T>;
223
+ /**
224
+ * Returns an iterator that computes the given mapper function using arguments
225
+ * from each of the iterables.
226
+ */
227
+ declare function imap<T, V>(iterable: Iterable<T>, mapper: (item: T) => V): Iterable<V>;
228
+ /**
229
+ * Returns an iterator that returns selected elements from the iterable. If
230
+ * `start` is non-zero, then elements from the iterable are skipped until start
231
+ * is reached. Then, elements are returned by making steps of `step` (defaults
232
+ * to 1). If set to higher than 1, items will be skipped. If `stop` is
233
+ * provided, then iteration continues until the iterator reached that index,
234
+ * otherwise, the iterable will be fully exhausted. `islice()` does not
235
+ * support negative values for `start`, `stop`, or `step`.
236
+ */
237
+ declare function islice<T>(iterable: Iterable<T>, stop: number): Iterable<T>;
238
+ declare function islice<T>(iterable: Iterable<T>, start: number, stop?: number | null, step?: number): Iterable<T>;
239
+ /**
240
+ * Returns an iterator that aggregates elements from each of the iterables.
241
+ * Used for lock-step iteration over several iterables at a time. When
242
+ * iterating over two iterables, use `izip2`. When iterating over three
243
+ * iterables, use `izip3`, etc. `izip` is an alias for `izip2`.
244
+ */
245
+ declare function izip2<T1, T2>(xs: Iterable<T1>, ys: Iterable<T2>): Iterable<[T1, T2]>;
246
+ /**
247
+ * Like izip2, but for three input iterables.
248
+ */
249
+ declare function izip3<T1, T2, T3>(xs: Iterable<T1>, ys: Iterable<T2>, zs: Iterable<T3>): Iterable<[T1, T2, T3]>;
250
+ declare const izip: typeof izip2;
251
+ /**
252
+ * Returns an iterator that aggregates elements from each of the iterables. If
253
+ * the iterables are of uneven length, missing values are filled-in with
254
+ * fillvalue. Iteration continues until the longest iterable is exhausted.
255
+ */
256
+ declare function izipLongest2<T1, T2, D>(xs: Iterable<T1>, ys: Iterable<T2>, filler?: D): Iterable<[T1 | D, T2 | D]>;
257
+ /**
258
+ * Like the other izips (`izip`, `izip3`, etc), but generalized to take an
259
+ * unlimited amount of input iterables. Think `izip(*iterables)` in Python.
260
+ *
261
+ * **Note:** Due to Flow type system limitations, you can only "generially" zip
262
+ * iterables with homogeneous types, so you cannot mix types like <A, B> like
263
+ * you can with izip2().
264
+ */
265
+ declare function izipMany<T>(...iters: Iterable<T>[]): Iterable<T[]>;
266
+ /**
267
+ * Return successive `r`-length permutations of elements in the iterable.
268
+ *
269
+ * If `r` is not specified, then `r` defaults to the length of the iterable and
270
+ * all possible full-length permutations are generated.
271
+ *
272
+ * Permutations are emitted in lexicographic sort order. So, if the input
273
+ * iterable is sorted, the permutation tuples will be produced in sorted order.
274
+ *
275
+ * Elements are treated as unique based on their position, not on their value.
276
+ * So if the input elements are unique, there will be no repeat values in each
277
+ * permutation.
278
+ */
279
+ declare function permutations<T>(iterable: Iterable<T>, r?: number): Iterable<T[]>;
280
+ /**
281
+ * Returns an iterator that produces elements from the iterable as long as the
282
+ * predicate is true.
283
+ */
284
+ declare function takewhile<T>(iterable: Iterable<T>, predicate: Predicate<T>): Iterable<T>;
285
+ declare function zipLongest2<T1, T2, D>(xs: Iterable<T1>, ys: Iterable<T2>, filler?: D): Array<[T1 | D, T2 | D]>;
286
+ declare const izipLongest: typeof izipLongest2;
287
+ declare const zipLongest: typeof zipLongest2;
288
+ declare function zipMany<T>(...iters: Iterable<T>[]): T[][];
289
+
290
+ /**
291
+ * Break iterable into lists of length `size`:
292
+ *
293
+ * [...chunked([1, 2, 3, 4, 5, 6], 3)]
294
+ * // [[1, 2, 3], [4, 5, 6]]
295
+ *
296
+ * If the length of iterable is not evenly divisible by `size`, the last returned
297
+ * list will be shorter:
298
+ *
299
+ * [...chunked([1, 2, 3, 4, 5, 6, 7, 8], 3)]
300
+ * // [[1, 2, 3], [4, 5, 6], [7, 8]]
301
+ */
302
+ declare function chunked<T>(iterable: Iterable<T>, size: number): Iterable<T[]>;
303
+ /**
304
+ * Return an iterator flattening one level of nesting in a list of lists:
305
+ *
306
+ * [...flatten([[0, 1], [2, 3]])]
307
+ * // [0, 1, 2, 3]
308
+ *
309
+ */
310
+ declare function flatten<T>(iterableOfIterables: Iterable<Iterable<T>>): Iterable<T>;
311
+ /**
312
+ * Returns an iterable containing only the first `n` elements of the given
313
+ * iterable.
314
+ */
315
+ declare function itake<T>(n: number, iterable: Iterable<T>): Iterable<T>;
316
+ /**
317
+ * Returns an iterator of paired items, overlapping, from the original. When
318
+ * the input iterable has a finite number of items `n`, the outputted iterable
319
+ * will have `n - 1` items.
320
+ *
321
+ * >>> pairwise([8, 2, 0, 7])
322
+ * [(8, 2), (2, 0), (0, 7)]
323
+ *
324
+ */
325
+ declare function pairwise<T>(iterable: Iterable<T>): Iterable<[T, T]>;
326
+ /**
327
+ * Returns a 2-tuple of arrays. Splits the elements in the input iterable into
328
+ * either of the two arrays. Will fully exhaust the input iterable. The first
329
+ * array contains all items that match the predicate, the second the rest:
330
+ *
331
+ * >>> const isOdd = x => x % 2 !== 0;
332
+ * >>> const iterable = range(10);
333
+ * >>> const [odds, evens] = partition(iterable, isOdd);
334
+ * >>> odds
335
+ * [1, 3, 5, 7, 9]
336
+ * >>> evens
337
+ * [0, 2, 4, 6, 8]
338
+ *
339
+ */
340
+ declare function partition<T>(iterable: Iterable<T>, predicate: Predicate<T>): [T[], T[]];
341
+ /**
342
+ * Yields the next item from each iterable in turn, alternating between them.
343
+ * Continues until all items are exhausted.
344
+ *
345
+ * >>> [...roundrobin([1, 2, 3], [4], [5, 6, 7, 8])]
346
+ * [1, 4, 5, 2, 6, 3, 7, 8]
347
+ */
348
+ declare function roundrobin<T>(...iters: Iterable<T>[]): Iterable<T>;
349
+ /**
350
+ * Yields the heads of all of the given iterables. This is almost like
351
+ * `roundrobin()`, except that the yielded outputs are grouped in to the
352
+ * "rounds":
353
+ *
354
+ * >>> [...heads([1, 2, 3], [4], [5, 6, 7, 8])]
355
+ * [[1, 4, 5], [2, 6], [3, 7], [8]]
356
+ *
357
+ * This is also different from `zipLongest()`, since the number of items in
358
+ * each round can decrease over time, rather than being filled with a filler.
359
+ */
360
+ declare function heads<T>(...iters: Array<Iterable<T>>): Iterable<T[]>;
361
+ /**
362
+ * Non-lazy version of itake().
363
+ */
364
+ declare function take<T>(n: number, iterable: Iterable<T>): T[];
365
+ /**
366
+ * Yield unique elements, preserving order.
367
+ *
368
+ * >>> [...uniqueEverseen('AAAABBBCCDAABBB')]
369
+ * ['A', 'B', 'C', 'D']
370
+ * >>> [...uniqueEverseen('AbBCcAB', s => s.toLowerCase())]
371
+ * ['A', 'b', 'C']
372
+ *
373
+ */
374
+ declare function uniqueEverseen<T>(iterable: Iterable<T>, keyFn?: (item: T) => Primitive): Iterable<T>;
375
+ /**
376
+ * Yields elements in order, ignoring serial duplicates.
377
+ *
378
+ * >>> [...uniqueJustseen('AAAABBBCCDAABBB')]
379
+ * ['A', 'B', 'C', 'D', 'A', 'B']
380
+ * >>> [...uniqueJustseen('AbBCcAB', s => s.toLowerCase())]
381
+ * ['A', 'b', 'C', 'A', 'B']
382
+ *
383
+ */
384
+ declare function uniqueJustseen<T>(iterable: Iterable<T>, keyFn?: (item: T) => Primitive): Iterable<T>;
385
+
386
+ /**
387
+ * Returns an iterable, filtering out any `undefined` values from the iterable.
388
+ *
389
+ * >>> compact([1, 2, undefined, 3])
390
+ * [1, 2, 3]
391
+ */
392
+ declare function icompact<T>(iterable: Iterable<T | null | undefined>): Iterable<T>;
393
+ /**
394
+ * See icompact().
395
+ */
396
+ declare function compact<T>(iterable: Iterable<T | null | undefined>): T[];
397
+ /**
398
+ * Removes all undefined values from the given object. Returns a new object.
399
+ *
400
+ * >>> compactObject({ a: 1, b: undefined, c: 0 })
401
+ * { a: 1, c: 0 }
402
+ *
403
+ */
404
+ declare function compactObject<K extends string, V>(obj: Record<K, V | null | undefined>): Record<K, V>;
405
+ /**
406
+ * Returns the first item in the iterable for which the predicate holds, if
407
+ * any. If no such item exists, `undefined` is returned. The default
408
+ * predicate is any defined value.
409
+ */
410
+ declare function find<T>(iterable: Iterable<T>, keyFn?: Predicate<T>): T | undefined;
411
+ /**
412
+ * Returns the first item in the iterable for which the predicate holds, if
413
+ * any. If no such item exists, `undefined` is returned. The default
414
+ * predicate is any defined value.
415
+ */
416
+ declare const first: typeof find;
417
+ /**
418
+ * Returns 0 or more values for every value in the given iterable.
419
+ * Technically, it's just calling map(), followed by flatten(), but it's a very
420
+ * useful operation if you want to map over a structure, but not have a 1:1
421
+ * input-output mapping. Instead, if you want to potentially return 0 or more
422
+ * values per input element, use flatmap():
423
+ *
424
+ * For example, to return all numbers `n` in the input iterable `n` times:
425
+ *
426
+ * >>> const repeatN = n => repeat(n, n);
427
+ * >>> [...flatmap([0, 1, 2, 3, 4], repeatN)]
428
+ * [1, 2, 2, 3, 3, 3, 4, 4, 4, 4] // note: no 0
429
+ *
430
+ */
431
+ declare function flatmap<T, S>(iterable: Iterable<T>, mapper: (item: T) => Iterable<S>): Iterable<S>;
432
+
433
+ export { Predicate, Primitive, all, any, chain, chunked, compact, compactObject, compress, contains, count, cycle, dropwhile, enumerate, filter, first, flatmap, flatten, groupby, heads, icompact, icompress, ifilter, imap, islice, itake, iter, izip, izip2, izip3, izipLongest, izipMany, map, max, min, pairwise, partition, permutations, range, reduce, roundrobin, sorted, sum, take, takewhile, uniqueEverseen, uniqueJustseen, zip, zip3, zipLongest, zipMany };