itertools-ts
Version:
Extended itertools port for TypeScript and JavaScript. Provides a huge set of functions for working with iterable collections (including async ones)
252 lines • 10.1 kB
JavaScript
var __asyncValues = (this && this.__asyncValues) || function (o) {
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
var m = o[Symbol.asyncIterator], i;
return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
};
var __await = (this && this.__await) || function (v) { return this instanceof __await ? (this.v = v, this) : new __await(v); }
var __asyncGenerator = (this && this.__asyncGenerator) || function (thisArg, _arguments, generator) {
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
var g = generator.apply(thisArg, _arguments || []), i, q = [];
return i = Object.create((typeof AsyncIterator === "function" ? AsyncIterator : Object).prototype), verb("next"), verb("throw"), verb("return", awaitReturn), i[Symbol.asyncIterator] = function () { return this; }, i;
function awaitReturn(f) { return function (v) { return Promise.resolve(v).then(f, reject); }; }
function verb(n, f) { if (g[n]) { i[n] = function (v) { return new Promise(function (a, b) { q.push([n, v, a, b]) > 1 || resume(n, v); }); }; if (f) i[n] = f(i[n]); } }
function resume(n, v) { try { step(g[n](v)); } catch (e) { settle(q[0][3], e); } }
function step(r) { r.value instanceof __await ? Promise.resolve(r.value.v).then(fulfill, reject) : settle(q[0][2], r); }
function fulfill(value) { resume("next", value); }
function reject(value) { resume("throw", value); }
function settle(f, v) { if (f(v), q.shift(), q.length) resume(q[0][0], q[0][1]); }
};
import { toAsyncIterable, toIterable } from "./transform";
import { createAsyncMultipleIterator, createMultipleIterator, MultipleIterationMode, } from "./tools";
/**
* Iterate multiple iterable collections simultaneously.
*
* Make an iterator that aggregates items from multiple iterators.
* Similar to Python's zip function.
*
* For uneven lengths, iterations stops when the shortest iterable is exhausted.
*
* @param iterables
*/
export function* zip(...iterables) {
for (const values of createMultipleIterator(MultipleIterationMode.SHORTEST, undefined, ...iterables)) {
yield values;
}
}
/**
* Iterate multiple async iterable collections simultaneously.
*
* Make an iterator that aggregates items from multiple iterators.
* Similar to Python's zip function.
*
* For uneven lengths, iterations stops when the shortest iterable is exhausted.
*
* @param iterables
*/
export function zipAsync(...iterables) {
return __asyncGenerator(this, arguments, function* zipAsync_1() {
var _a, e_1, _b, _c;
try {
for (var _d = true, _e = __asyncValues(createAsyncMultipleIterator(MultipleIterationMode.SHORTEST, undefined, ...iterables)), _f; _f = yield __await(_e.next()), _a = _f.done, !_a; _d = true) {
_c = _f.value;
_d = false;
const values = _c;
yield yield __await(values);
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (!_d && !_a && (_b = _e.return)) yield __await(_b.call(_e));
}
finally { if (e_1) throw e_1.error; }
}
});
}
/**
* Iterate multiple iterable collections simultaneously.
*
* Make an iterable that aggregates items from multiple iterators.
* Similar to Python's zip_longest function.
*
* Iteration continues until the longest iterable is exhausted.
* For uneven lengths, the exhausted iterables will produce `filler` value for the remaining iterations.
*
* @param filler
* @param iterables
*/
export function* zipFilled(filler, ...iterables) {
for (const values of createMultipleIterator(MultipleIterationMode.LONGEST, filler, ...iterables)) {
yield values;
}
}
/**
* Iterate multiple async iterable collections simultaneously.
*
* Make an iterable that aggregates items from multiple iterators.
* Similar to Python's zip_longest function.
*
* Iteration continues until the longest iterable is exhausted.
* For uneven lengths, the exhausted iterables will produce `filler` value for the remaining iterations.
*
* @param filler
* @param iterables
*/
export function zipFilledAsync(filler, ...iterables) {
return __asyncGenerator(this, arguments, function* zipFilledAsync_1() {
var _a, e_2, _b, _c;
try {
for (var _d = true, _e = __asyncValues(createAsyncMultipleIterator(MultipleIterationMode.LONGEST, filler, ...iterables)), _f; _f = yield __await(_e.next()), _a = _f.done, !_a; _d = true) {
_c = _f.value;
_d = false;
const values = _c;
yield yield __await(values);
}
}
catch (e_2_1) { e_2 = { error: e_2_1 }; }
finally {
try {
if (!_d && !_a && (_b = _e.return)) yield __await(_b.call(_e));
}
finally { if (e_2) throw e_2.error; }
}
});
}
/**
* Iterate multiple iterable collections simultaneously.
*
* Make an iterable that aggregates items from multiple iterators.
* Similar to Python's zip_longest function.
*
* Iteration continues until the longest iterable is exhausted.
* For uneven lengths, the exhausted iterables will produce `undefined` for the remaining iterations.
*
* @param iterables
*/
export function* zipLongest(...iterables) {
for (const values of createMultipleIterator(MultipleIterationMode.LONGEST, undefined, ...iterables)) {
yield values;
}
}
/**
* Iterate multiple async iterable collections simultaneously.
*
* Make an iterable that aggregates items from multiple iterators.
* Similar to Python's zip_longest function.
*
* Iteration continues until the longest iterable is exhausted.
* For uneven lengths, the exhausted iterables will produce `undefined` for the remaining iterations.
*
* @param iterables
*/
export function zipLongestAsync(...iterables) {
return __asyncGenerator(this, arguments, function* zipLongestAsync_1() {
var _a, e_3, _b, _c;
try {
for (var _d = true, _e = __asyncValues(createAsyncMultipleIterator(MultipleIterationMode.LONGEST, undefined, ...iterables)), _f; _f = yield __await(_e.next()), _a = _f.done, !_a; _d = true) {
_c = _f.value;
_d = false;
const values = _c;
yield yield __await(values);
}
}
catch (e_3_1) { e_3 = { error: e_3_1 }; }
finally {
try {
if (!_d && !_a && (_b = _e.return)) yield __await(_b.call(_e));
}
finally { if (e_3) throw e_3.error; }
}
});
}
/**
* Iterate multiple iterable collections of equal lengths simultaneously.
*
* Works like multi.zip() method but throws LengthException if lengths not equal,
* i.e., at least one iterator ends before the others.
*
* @param iterables
*
* @throws LengthError if iterators lengths not equal.
*/
export function* zipEqual(...iterables) {
for (const values of createMultipleIterator(MultipleIterationMode.STRICT_EQUAL, undefined, ...iterables)) {
yield values;
}
}
/**
* Iterate multiple async iterable collections of equal lengths simultaneously.
*
* Works like multi.zipAsync() method but throws LengthException if lengths not equal,
* i.e., at least one iterator ends before the others.
*
* @param iterables
*
* @throws LengthError if iterators lengths not equal.
*/
export function zipEqualAsync(...iterables) {
return __asyncGenerator(this, arguments, function* zipEqualAsync_1() {
var _a, e_4, _b, _c;
try {
for (var _d = true, _e = __asyncValues(createAsyncMultipleIterator(MultipleIterationMode.STRICT_EQUAL, undefined, ...iterables)), _f; _f = yield __await(_e.next()), _a = _f.done, !_a; _d = true) {
_c = _f.value;
_d = false;
const values = _c;
yield yield __await(values);
}
}
catch (e_4_1) { e_4 = { error: e_4_1 }; }
finally {
try {
if (!_d && !_a && (_b = _e.return)) yield __await(_b.call(_e));
}
finally { if (e_4) throw e_4.error; }
}
});
}
/**
* Chain multiple iterables together into a single iteration.
*
* Makes a single continuous sequence out of multiple sequences.
*
* @param iterables
*/
export function* chain(...iterables) {
for (const iterable of iterables) {
for (const item of toIterable(iterable)) {
yield item;
}
}
}
/**
* Chain multiple async iterables together into a single iteration.
*
* Makes a single continuous sequence out of multiple sequences.
*
* @param iterables
*/
export function chainAsync(...iterables) {
return __asyncGenerator(this, arguments, function* chainAsync_1() {
var _a, e_5, _b, _c;
for (const iterable of iterables) {
try {
for (var _d = true, _e = (e_5 = void 0, __asyncValues(toAsyncIterable(iterable))), _f; _f = yield __await(_e.next()), _a = _f.done, !_a; _d = true) {
_c = _f.value;
_d = false;
const item = _c;
yield yield __await(item);
}
}
catch (e_5_1) { e_5 = { error: e_5_1 }; }
finally {
try {
if (!_d && !_a && (_b = _e.return)) yield __await(_b.call(_e));
}
finally { if (e_5) throw e_5.error; }
}
}
});
}
//# sourceMappingURL=multi.js.map