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/builtins.js.flow
DELETED
|
@@ -1,294 +0,0 @@
|
|
|
1
|
-
// @flow strict
|
|
2
|
-
|
|
3
|
-
import { first } from './custom';
|
|
4
|
-
import { count, ifilter, imap, izip, izip3, takewhile } from './itertools';
|
|
5
|
-
import type { Maybe, Predicate, Primitive } from './types';
|
|
6
|
-
import { identityPredicate, keyToCmp, numberIdentity, primitiveIdentity } from './utils';
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* Returns true when all of the items in iterable are truthy. An optional key
|
|
10
|
-
* function can be used to define what truthiness means for this specific
|
|
11
|
-
* collection.
|
|
12
|
-
*
|
|
13
|
-
* Examples:
|
|
14
|
-
*
|
|
15
|
-
* all([]) // => true
|
|
16
|
-
* all([0]) // => false
|
|
17
|
-
* all([0, 1, 2]) // => false
|
|
18
|
-
* all([1, 2, 3]) // => true
|
|
19
|
-
*
|
|
20
|
-
* Examples with using a key function:
|
|
21
|
-
*
|
|
22
|
-
* all([2, 4, 6], n => n % 2 === 0) // => true
|
|
23
|
-
* all([2, 4, 5], n => n % 2 === 0) // => false
|
|
24
|
-
*
|
|
25
|
-
*/
|
|
26
|
-
export function all<T>(iterable: Iterable<T>, keyFn: Predicate<T> = identityPredicate): boolean {
|
|
27
|
-
for (let item of iterable) {
|
|
28
|
-
if (!keyFn(item)) {
|
|
29
|
-
return false;
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
return true;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* Returns true when any of the items in iterable are truthy. An optional key
|
|
38
|
-
* function can be used to define what truthiness means for this specific
|
|
39
|
-
* collection.
|
|
40
|
-
*
|
|
41
|
-
* Examples:
|
|
42
|
-
*
|
|
43
|
-
* any([]) // => false
|
|
44
|
-
* any([0]) // => false
|
|
45
|
-
* any([0, 1, null, undefined]) // => true
|
|
46
|
-
*
|
|
47
|
-
* Examples with using a key function:
|
|
48
|
-
*
|
|
49
|
-
* any([1, 4, 5], n => n % 2 === 0) // => true
|
|
50
|
-
* any([{name: 'Bob'}, {name: 'Alice'}], person => person.name.startsWith('C')) // => false
|
|
51
|
-
*
|
|
52
|
-
*/
|
|
53
|
-
export function any<T>(iterable: Iterable<T>, keyFn: Predicate<T> = identityPredicate): boolean {
|
|
54
|
-
for (let item of iterable) {
|
|
55
|
-
if (keyFn(item)) {
|
|
56
|
-
return true;
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
return false;
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
/**
|
|
64
|
-
* Returns true when any of the items in the iterable are equal to the target object.
|
|
65
|
-
*
|
|
66
|
-
* Examples:
|
|
67
|
-
*
|
|
68
|
-
* contains([], 'whatever') // => false
|
|
69
|
-
* contains([3], 42) // => false
|
|
70
|
-
* contains([3], 3) // => true
|
|
71
|
-
* contains([0, 1, 2], 2) // => true
|
|
72
|
-
*
|
|
73
|
-
*/
|
|
74
|
-
export function contains<T>(haystack: Iterable<T>, needle: T): boolean {
|
|
75
|
-
return any(haystack, (x) => x === needle);
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
/**
|
|
79
|
-
* Returns an iterable of enumeration pairs. Iterable must be a sequence, an
|
|
80
|
-
* iterator, or some other object which supports iteration. The elements
|
|
81
|
-
* produced by returns a tuple containing a counter value (starting from 0 by
|
|
82
|
-
* default) and the values obtained from iterating over given iterable.
|
|
83
|
-
*
|
|
84
|
-
* Example:
|
|
85
|
-
*
|
|
86
|
-
* import { enumerate } from 'itertools';
|
|
87
|
-
*
|
|
88
|
-
* console.log([...enumerate(['hello', 'world'])]);
|
|
89
|
-
* // [0, 'hello'], [1, 'world']]
|
|
90
|
-
*/
|
|
91
|
-
export function* enumerate<T>(iterable: Iterable<T>, start: number = 0): Iterable<[number, T]> {
|
|
92
|
-
let index: number = start;
|
|
93
|
-
for (let value of iterable) {
|
|
94
|
-
yield [index++, value];
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
/**
|
|
99
|
-
* Non-lazy version of ifilter().
|
|
100
|
-
*/
|
|
101
|
-
export function filter<T>(iterable: Iterable<T>, predicate: Predicate<T>): Array<T> {
|
|
102
|
-
return Array.from(ifilter(iterable, predicate));
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
/**
|
|
106
|
-
* Returns an iterator object for the given iterable. This can be used to
|
|
107
|
-
* manually get an iterator for any iterable datastructure. The purpose and
|
|
108
|
-
* main use case of this function is to get a single iterator (a thing with
|
|
109
|
-
* state, think of it as a "cursor") which can only be consumed once.
|
|
110
|
-
*/
|
|
111
|
-
export function iter<T>(iterable: Iterable<T>): Iterator<T> {
|
|
112
|
-
// TODO: Not sure why Flow choked on this expression below, but at least we lock down the
|
|
113
|
-
// type transformation in the function signature this way.
|
|
114
|
-
// $FlowFixMe[incompatible-use]
|
|
115
|
-
return iterable[Symbol.iterator]();
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
/**
|
|
119
|
-
* Non-lazy version of imap().
|
|
120
|
-
*/
|
|
121
|
-
export function map<T, V>(iterable: Iterable<T>, mapper: (T) => V): Array<V> {
|
|
122
|
-
return Array.from(imap(iterable, mapper));
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
/**
|
|
126
|
-
* Return the largest item in an iterable. Only works for numbers, as ordering
|
|
127
|
-
* is pretty poorly defined on any other data type in JS. The optional `keyFn`
|
|
128
|
-
* argument specifies a one-argument ordering function like that used for
|
|
129
|
-
* sorted().
|
|
130
|
-
*
|
|
131
|
-
* If the iterable is empty, `undefined` is returned.
|
|
132
|
-
*
|
|
133
|
-
* If multiple items are maximal, the function returns either one of them, but
|
|
134
|
-
* which one is not defined.
|
|
135
|
-
*/
|
|
136
|
-
export function max<T>(iterable: Iterable<T>, keyFn: (T) => number = numberIdentity): Maybe<T> {
|
|
137
|
-
return reduce_(iterable, (x, y) => (keyFn(x) > keyFn(y) ? x : y));
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
/**
|
|
141
|
-
* Return the smallest item in an iterable. Only works for numbers, as
|
|
142
|
-
* ordering is pretty poorly defined on any other data type in JS. The
|
|
143
|
-
* optional `keyFn` argument specifies a one-argument ordering function like
|
|
144
|
-
* that used for sorted().
|
|
145
|
-
*
|
|
146
|
-
* If the iterable is empty, `undefined` is returned.
|
|
147
|
-
*
|
|
148
|
-
* If multiple items are minimal, the function returns either one of them, but
|
|
149
|
-
* which one is not defined.
|
|
150
|
-
*/
|
|
151
|
-
export function min<T>(iterable: Iterable<T>, keyFn: (T) => number = numberIdentity): Maybe<T> {
|
|
152
|
-
return reduce_(iterable, (x, y) => (keyFn(x) < keyFn(y) ? x : y));
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
/**
|
|
156
|
-
* Internal helper for the range function
|
|
157
|
-
*/
|
|
158
|
-
function _range(start: number, stop: number, step: number): Iterable<number> {
|
|
159
|
-
const counter = count(start, step);
|
|
160
|
-
const pred = step >= 0 ? (n) => n < stop : (n) => n > stop;
|
|
161
|
-
return takewhile(counter, pred);
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
/**
|
|
165
|
-
* Returns an iterator producing all the numbers in the given range one by one,
|
|
166
|
-
* starting from `start` (default 0), as long as `i < stop`, in increments of
|
|
167
|
-
* `step` (default 1).
|
|
168
|
-
*
|
|
169
|
-
* `range(a)` is a convenient shorthand for `range(0, a)`.
|
|
170
|
-
*
|
|
171
|
-
* Various valid invocations:
|
|
172
|
-
*
|
|
173
|
-
* range(5) // [0, 1, 2, 3, 4]
|
|
174
|
-
* range(2, 5) // [2, 3, 4]
|
|
175
|
-
* range(0, 5, 2) // [0, 2, 4]
|
|
176
|
-
* range(5, 0, -1) // [5, 4, 3, 2, 1]
|
|
177
|
-
* range(-3) // []
|
|
178
|
-
*
|
|
179
|
-
* For a positive `step`, the iterator will keep producing values `n` as long
|
|
180
|
-
* as the stop condition `n < stop` is satisfied.
|
|
181
|
-
*
|
|
182
|
-
* For a negative `step`, the iterator will keep producing values `n` as long
|
|
183
|
-
* as the stop condition `n > stop` is satisfied.
|
|
184
|
-
*
|
|
185
|
-
* The produced range will be empty if the first value to produce already does
|
|
186
|
-
* not meet the value constraint.
|
|
187
|
-
*/
|
|
188
|
-
export function range(a: number, ...rest: Array<number>): Iterable<number> {
|
|
189
|
-
const args = [a, ...rest]; // "a" was only used by Flow to make at least one value mandatory
|
|
190
|
-
switch (args.length) {
|
|
191
|
-
case 1:
|
|
192
|
-
return _range(0, args[0], 1);
|
|
193
|
-
case 2:
|
|
194
|
-
return _range(args[0], args[1], 1);
|
|
195
|
-
case 3:
|
|
196
|
-
return _range(args[0], args[1], args[2]);
|
|
197
|
-
/* istanbul ignore next */
|
|
198
|
-
default:
|
|
199
|
-
throw new Error('invalid number of arguments');
|
|
200
|
-
}
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
/**
|
|
204
|
-
* Apply function of two arguments cumulatively to the items of sequence, from
|
|
205
|
-
* left to right, so as to reduce the sequence to a single value. For example:
|
|
206
|
-
*
|
|
207
|
-
* reduce([1, 2, 3, 4, 5], (x, y) => x + y, 0)
|
|
208
|
-
*
|
|
209
|
-
* calculates
|
|
210
|
-
*
|
|
211
|
-
* (((((0+1)+2)+3)+4)+5)
|
|
212
|
-
*
|
|
213
|
-
* The left argument, `x`, is the accumulated value and the right argument,
|
|
214
|
-
* `y`, is the update value from the sequence.
|
|
215
|
-
*
|
|
216
|
-
* **Difference between `reduce()` and `reduce\_()`**: `reduce()` requires an
|
|
217
|
-
* explicit initializer, whereas `reduce_()` will automatically use the first
|
|
218
|
-
* item in the given iterable as the initializer. When using `reduce()`, the
|
|
219
|
-
* initializer value is placed before the items of the sequence in the
|
|
220
|
-
* calculation, and serves as a default when the sequence is empty. When using
|
|
221
|
-
* `reduce_()`, and the given iterable is empty, then no default value can be
|
|
222
|
-
* derived and `undefined` will be returned.
|
|
223
|
-
*/
|
|
224
|
-
export function reduce<T, O>(iterable: Iterable<T>, reducer: (O, T, number) => O, start: O): O {
|
|
225
|
-
const it = iter(iterable);
|
|
226
|
-
let output = start;
|
|
227
|
-
for (const [index, item] of enumerate(it)) {
|
|
228
|
-
output = reducer(output, item, index);
|
|
229
|
-
}
|
|
230
|
-
return output;
|
|
231
|
-
}
|
|
232
|
-
|
|
233
|
-
/**
|
|
234
|
-
* See reduce().
|
|
235
|
-
*/
|
|
236
|
-
export function reduce_<T>(iterable: Iterable<T>, reducer: (T, T, number) => T): Maybe<T> {
|
|
237
|
-
const it = iter(iterable);
|
|
238
|
-
const start = first(it);
|
|
239
|
-
if (start === undefined) {
|
|
240
|
-
return undefined;
|
|
241
|
-
} else {
|
|
242
|
-
return reduce(it, reducer, start);
|
|
243
|
-
}
|
|
244
|
-
}
|
|
245
|
-
|
|
246
|
-
/**
|
|
247
|
-
* Return a new sorted list from the items in iterable.
|
|
248
|
-
*
|
|
249
|
-
* Has two optional arguments:
|
|
250
|
-
*
|
|
251
|
-
* * `keyFn` specifies a function of one argument providing a primitive
|
|
252
|
-
* identity for each element in the iterable. that will be used to compare.
|
|
253
|
-
* The default value is to use a default identity function that is only
|
|
254
|
-
* defined for primitive types.
|
|
255
|
-
*
|
|
256
|
-
* * `reverse` is a boolean value. If `true`, then the list elements are
|
|
257
|
-
* sorted as if each comparison were reversed.
|
|
258
|
-
*/
|
|
259
|
-
export function sorted<T>(
|
|
260
|
-
iterable: Iterable<T>,
|
|
261
|
-
keyFn: (T) => Primitive = primitiveIdentity,
|
|
262
|
-
reverse: boolean = false
|
|
263
|
-
): Array<T> {
|
|
264
|
-
const result = Array.from(iterable);
|
|
265
|
-
result.sort(keyToCmp(keyFn)); // sort in-place
|
|
266
|
-
|
|
267
|
-
if (reverse) {
|
|
268
|
-
result.reverse(); // reverse in-place
|
|
269
|
-
}
|
|
270
|
-
|
|
271
|
-
return result;
|
|
272
|
-
}
|
|
273
|
-
|
|
274
|
-
/**
|
|
275
|
-
* Sums the items of an iterable from left to right and returns the total. The
|
|
276
|
-
* sum will defaults to 0 if the iterable is empty.
|
|
277
|
-
*/
|
|
278
|
-
export function sum(iterable: Iterable<number>): number {
|
|
279
|
-
return reduce(iterable, (x, y) => x + y, 0);
|
|
280
|
-
}
|
|
281
|
-
|
|
282
|
-
/**
|
|
283
|
-
* See izip.
|
|
284
|
-
*/
|
|
285
|
-
export function zip<T1, T2>(xs: Iterable<T1>, ys: Iterable<T2>): Array<[T1, T2]> {
|
|
286
|
-
return Array.from(izip(xs, ys));
|
|
287
|
-
}
|
|
288
|
-
|
|
289
|
-
/**
|
|
290
|
-
* See izip3.
|
|
291
|
-
*/
|
|
292
|
-
export function zip3<T1, T2, T3>(xs: Iterable<T1>, ys: Iterable<T2>, zs: Iterable<T3>): Array<[T1, T2, T3]> {
|
|
293
|
-
return Array.from(izip3(xs, ys, zs));
|
|
294
|
-
}
|
package/custom.d.ts
DELETED
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
import { Maybe, Predicate } from './types';
|
|
2
|
-
|
|
3
|
-
export function icompact<T>(iterable: Iterable<T>): Iterable<NonNullable<T>>;
|
|
4
|
-
export function compact<T>(iterable: Iterable<T>): Array<NonNullable<T>>;
|
|
5
|
-
export function compactObject<O extends object>(obj: O): { [K in keyof O]: NonNullable<O[K]> };
|
|
6
|
-
export function first<T>(iterable: Iterable<T>, keyFn?: Predicate<T>): Maybe<T>;
|
|
7
|
-
export function flatmap<T, S>(iterable: Iterable<T>, mapper: (item: T) => Iterable<S>): Iterable<S>;
|
package/custom.js
DELETED
|
@@ -1,186 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
var _regeneratorRuntime2 = require("@babel/runtime/regenerator");
|
|
4
|
-
|
|
5
|
-
Object.defineProperty(exports, "__esModule", {
|
|
6
|
-
value: true
|
|
7
|
-
});
|
|
8
|
-
exports.icompact = icompact;
|
|
9
|
-
exports.compact = compact;
|
|
10
|
-
exports.compactObject = compactObject;
|
|
11
|
-
exports.first = first;
|
|
12
|
-
exports.flatmap = flatmap;
|
|
13
|
-
|
|
14
|
-
var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
|
|
15
|
-
|
|
16
|
-
var _itertools = require("./itertools");
|
|
17
|
-
|
|
18
|
-
var _moreItertools = require("./more-itertools");
|
|
19
|
-
|
|
20
|
-
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
|
|
21
|
-
|
|
22
|
-
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
|
|
23
|
-
|
|
24
|
-
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
|
|
25
|
-
|
|
26
|
-
function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
|
|
27
|
-
|
|
28
|
-
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
|
|
29
|
-
|
|
30
|
-
var _marked = /*#__PURE__*/_regeneratorRuntime2.mark(icompact);
|
|
31
|
-
|
|
32
|
-
function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e2) { throw _e2; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e3) { didErr = true; err = _e3; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; }
|
|
33
|
-
|
|
34
|
-
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
|
|
35
|
-
|
|
36
|
-
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
|
|
37
|
-
|
|
38
|
-
function isDefined(x) {
|
|
39
|
-
return x !== undefined;
|
|
40
|
-
}
|
|
41
|
-
/**
|
|
42
|
-
* Returns an iterable, filtering out any `undefined` values from the input
|
|
43
|
-
* iterable. This function is useful to convert a list of `Maybe<T>`'s to
|
|
44
|
-
* a list of `T`'s, discarding all the undefined values:
|
|
45
|
-
*
|
|
46
|
-
* >>> compact([1, 2, undefined, 3])
|
|
47
|
-
* [1, 2, 3]
|
|
48
|
-
*/
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
function icompact(iterable) {
|
|
52
|
-
var _iterator, _step, item;
|
|
53
|
-
|
|
54
|
-
return _regenerator["default"].wrap(function icompact$(_context) {
|
|
55
|
-
while (1) {
|
|
56
|
-
switch (_context.prev = _context.next) {
|
|
57
|
-
case 0:
|
|
58
|
-
_iterator = _createForOfIteratorHelper(iterable);
|
|
59
|
-
_context.prev = 1;
|
|
60
|
-
|
|
61
|
-
_iterator.s();
|
|
62
|
-
|
|
63
|
-
case 3:
|
|
64
|
-
if ((_step = _iterator.n()).done) {
|
|
65
|
-
_context.next = 10;
|
|
66
|
-
break;
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
item = _step.value;
|
|
70
|
-
|
|
71
|
-
if (!(typeof item !== 'undefined')) {
|
|
72
|
-
_context.next = 8;
|
|
73
|
-
break;
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
_context.next = 8;
|
|
77
|
-
return item;
|
|
78
|
-
|
|
79
|
-
case 8:
|
|
80
|
-
_context.next = 3;
|
|
81
|
-
break;
|
|
82
|
-
|
|
83
|
-
case 10:
|
|
84
|
-
_context.next = 15;
|
|
85
|
-
break;
|
|
86
|
-
|
|
87
|
-
case 12:
|
|
88
|
-
_context.prev = 12;
|
|
89
|
-
_context.t0 = _context["catch"](1);
|
|
90
|
-
|
|
91
|
-
_iterator.e(_context.t0);
|
|
92
|
-
|
|
93
|
-
case 15:
|
|
94
|
-
_context.prev = 15;
|
|
95
|
-
|
|
96
|
-
_iterator.f();
|
|
97
|
-
|
|
98
|
-
return _context.finish(15);
|
|
99
|
-
|
|
100
|
-
case 18:
|
|
101
|
-
case "end":
|
|
102
|
-
return _context.stop();
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
}, _marked, null, [[1, 12, 15, 18]]);
|
|
106
|
-
}
|
|
107
|
-
/**
|
|
108
|
-
* See icompact().
|
|
109
|
-
*/
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
function compact(iterable) {
|
|
113
|
-
return Array.from(icompact(iterable));
|
|
114
|
-
}
|
|
115
|
-
/**
|
|
116
|
-
* Removes all undefined values from the given object. Returns a new object.
|
|
117
|
-
*
|
|
118
|
-
* >>> compactObject({ a: 1, b: undefined, c: 0 })
|
|
119
|
-
* { a: 1, c: 0 }
|
|
120
|
-
*
|
|
121
|
-
*/
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
function compactObject(obj) {
|
|
125
|
-
var result = {};
|
|
126
|
-
|
|
127
|
-
for (var _i = 0, _Object$entries = Object.entries(obj); _i < _Object$entries.length; _i++) {
|
|
128
|
-
var _Object$entries$_i = _slicedToArray(_Object$entries[_i], 2),
|
|
129
|
-
_key = _Object$entries$_i[0],
|
|
130
|
-
value = _Object$entries$_i[1];
|
|
131
|
-
|
|
132
|
-
if (typeof value !== 'undefined') {
|
|
133
|
-
result[_key] = value;
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
return result;
|
|
138
|
-
}
|
|
139
|
-
/**
|
|
140
|
-
* Returns the first item in the iterable for which the predicate holds, if
|
|
141
|
-
* any. If no such item exists, `undefined` is returned. The default
|
|
142
|
-
* predicate is any defined value.
|
|
143
|
-
*/
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
function first(iterable, keyFn) {
|
|
147
|
-
var fn = keyFn || isDefined;
|
|
148
|
-
|
|
149
|
-
var _iterator2 = _createForOfIteratorHelper(iterable),
|
|
150
|
-
_step2;
|
|
151
|
-
|
|
152
|
-
try {
|
|
153
|
-
for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
|
|
154
|
-
var value = _step2.value;
|
|
155
|
-
|
|
156
|
-
if (fn(value)) {
|
|
157
|
-
return value;
|
|
158
|
-
}
|
|
159
|
-
}
|
|
160
|
-
} catch (err) {
|
|
161
|
-
_iterator2.e(err);
|
|
162
|
-
} finally {
|
|
163
|
-
_iterator2.f();
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
return undefined;
|
|
167
|
-
}
|
|
168
|
-
/**
|
|
169
|
-
* Returns 0 or more values for every value in the given iterable.
|
|
170
|
-
* Technically, it's just calling map(), followed by flatten(), but it's a very
|
|
171
|
-
* useful operation if you want to map over a structure, but not have a 1:1
|
|
172
|
-
* input-output mapping. Instead, if you want to potentially return 0 or more
|
|
173
|
-
* values per input element, use flatmap():
|
|
174
|
-
*
|
|
175
|
-
* For example, to return all numbers `n` in the input iterable `n` times:
|
|
176
|
-
*
|
|
177
|
-
* >>> const repeatN = n => repeat(n, n);
|
|
178
|
-
* >>> [...flatmap([0, 1, 2, 3, 4], repeatN)]
|
|
179
|
-
* [1, 2, 2, 3, 3, 3, 4, 4, 4, 4] // note: no 0
|
|
180
|
-
*
|
|
181
|
-
*/
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
function flatmap(iterable, mapper) {
|
|
185
|
-
return (0, _moreItertools.flatten)((0, _itertools.imap)(iterable, mapper));
|
|
186
|
-
}
|
package/custom.js.flow
DELETED
|
@@ -1,82 +0,0 @@
|
|
|
1
|
-
// @flow strict
|
|
2
|
-
|
|
3
|
-
import { imap } from './itertools';
|
|
4
|
-
import { flatten } from './more-itertools';
|
|
5
|
-
import type { Maybe, Predicate } from './types';
|
|
6
|
-
|
|
7
|
-
function isDefined<T>(x: T): boolean {
|
|
8
|
-
return x !== undefined;
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* Returns an iterable, filtering out any `undefined` values from the input
|
|
13
|
-
* iterable. This function is useful to convert a list of `Maybe<T>`'s to
|
|
14
|
-
* a list of `T`'s, discarding all the undefined values:
|
|
15
|
-
*
|
|
16
|
-
* >>> compact([1, 2, undefined, 3])
|
|
17
|
-
* [1, 2, 3]
|
|
18
|
-
*/
|
|
19
|
-
export function* icompact<T>(iterable: Iterable<T>): Iterable<$NonMaybeType<T>> {
|
|
20
|
-
for (let item of iterable) {
|
|
21
|
-
if (typeof item !== 'undefined') {
|
|
22
|
-
yield item;
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* See icompact().
|
|
29
|
-
*/
|
|
30
|
-
export function compact<T>(iterable: Iterable<T>): Array<$NonMaybeType<T>> {
|
|
31
|
-
return Array.from(icompact(iterable));
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
* Removes all undefined values from the given object. Returns a new object.
|
|
36
|
-
*
|
|
37
|
-
* >>> compactObject({ a: 1, b: undefined, c: 0 })
|
|
38
|
-
* { a: 1, c: 0 }
|
|
39
|
-
*
|
|
40
|
-
*/
|
|
41
|
-
export function compactObject<O: { +[key: string]: mixed }>(obj: O): $ObjMap<O, <T>(T) => $NonMaybeType<T>> {
|
|
42
|
-
let result = {};
|
|
43
|
-
for (const [key, value] of Object.entries(obj)) {
|
|
44
|
-
if (typeof value !== 'undefined') {
|
|
45
|
-
result[key] = value;
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
return result;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
/**
|
|
52
|
-
* Returns the first item in the iterable for which the predicate holds, if
|
|
53
|
-
* any. If no such item exists, `undefined` is returned. The default
|
|
54
|
-
* predicate is any defined value.
|
|
55
|
-
*/
|
|
56
|
-
export function first<T>(iterable: Iterable<T>, keyFn?: Predicate<T>): Maybe<T> {
|
|
57
|
-
const fn = keyFn || isDefined;
|
|
58
|
-
for (let value of iterable) {
|
|
59
|
-
if (fn(value)) {
|
|
60
|
-
return value;
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
return undefined;
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
/**
|
|
67
|
-
* Returns 0 or more values for every value in the given iterable.
|
|
68
|
-
* Technically, it's just calling map(), followed by flatten(), but it's a very
|
|
69
|
-
* useful operation if you want to map over a structure, but not have a 1:1
|
|
70
|
-
* input-output mapping. Instead, if you want to potentially return 0 or more
|
|
71
|
-
* values per input element, use flatmap():
|
|
72
|
-
*
|
|
73
|
-
* For example, to return all numbers `n` in the input iterable `n` times:
|
|
74
|
-
*
|
|
75
|
-
* >>> const repeatN = n => repeat(n, n);
|
|
76
|
-
* >>> [...flatmap([0, 1, 2, 3, 4], repeatN)]
|
|
77
|
-
* [1, 2, 2, 3, 3, 3, 4, 4, 4, 4] // note: no 0
|
|
78
|
-
*
|
|
79
|
-
*/
|
|
80
|
-
export function flatmap<T, S>(iterable: Iterable<T>, mapper: (T) => Iterable<S>): Iterable<S> {
|
|
81
|
-
return flatten(imap(iterable, mapper));
|
|
82
|
-
}
|
package/index.d.ts
DELETED
|
@@ -1,52 +0,0 @@
|
|
|
1
|
-
export { Predicate, Primitive } from './types';
|
|
2
|
-
|
|
3
|
-
export {
|
|
4
|
-
all,
|
|
5
|
-
any,
|
|
6
|
-
contains,
|
|
7
|
-
enumerate,
|
|
8
|
-
filter,
|
|
9
|
-
iter,
|
|
10
|
-
map,
|
|
11
|
-
max,
|
|
12
|
-
min,
|
|
13
|
-
range,
|
|
14
|
-
reduce,
|
|
15
|
-
sorted,
|
|
16
|
-
sum,
|
|
17
|
-
zip,
|
|
18
|
-
zip3,
|
|
19
|
-
} from './builtins';
|
|
20
|
-
export {
|
|
21
|
-
chain,
|
|
22
|
-
compress,
|
|
23
|
-
count,
|
|
24
|
-
cycle,
|
|
25
|
-
dropwhile,
|
|
26
|
-
groupby,
|
|
27
|
-
icompress,
|
|
28
|
-
ifilter,
|
|
29
|
-
imap,
|
|
30
|
-
izip,
|
|
31
|
-
izip2,
|
|
32
|
-
izip3,
|
|
33
|
-
izipMany,
|
|
34
|
-
izipLongest,
|
|
35
|
-
permutations,
|
|
36
|
-
takewhile,
|
|
37
|
-
zipLongest,
|
|
38
|
-
zipMany,
|
|
39
|
-
} from './itertools';
|
|
40
|
-
export {
|
|
41
|
-
chunked,
|
|
42
|
-
flatten,
|
|
43
|
-
heads,
|
|
44
|
-
itake,
|
|
45
|
-
pairwise,
|
|
46
|
-
partition,
|
|
47
|
-
roundrobin,
|
|
48
|
-
take,
|
|
49
|
-
uniqueEverseen,
|
|
50
|
-
uniqueJustseen,
|
|
51
|
-
} from './more-itertools';
|
|
52
|
-
export { compact, compactObject, first, flatmap, icompact } from './custom';
|