UNPKG

lilit

Version:

[![Build status][tr-svg]][travis]

756 lines 19.4 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const common_1 = require("./common"); // OPERATORS function map(f) { return async function* (xs) { for await (const x of xs) yield f(x); }; } exports.map = map; function tap(f) { return async function* (xs) { for await (const x of xs) { f(x); yield x; } }; } exports.tap = tap; exports.inspect = tap; function forEach(f) { return async function (xs) { for await (const x of xs) f(x); }; } exports.forEach = forEach; exports.subscribe = forEach; function reduce(f, init) { return async function (xs) { let res = init; for await (const x of xs) { res = f(res, x); } return res; }; } exports.reduce = reduce; function scan(f, init) { return async function* (xs) { let res = init; for await (const x of xs) { res = f(res, x); yield res; } }; } exports.scan = scan; exports.reducutions = scan; function some(p) { return async function (xs) { for await (const x of xs) { if (p(x)) return true; } return false; }; } exports.some = some; function every(p) { return async function (xs) { for await (const x of xs) { if (!p(x)) return false; } return true; }; } exports.every = every; function filter(p) { return async function* (xs) { for await (const x of xs) { if (p(x)) yield x; } }; } exports.filter = filter; function partition(p) { return function (xs) { const [xs1, xs2] = common_1.asyncTee(xs); return [filter(p)(xs1), filter((x) => !p(x))(xs2)]; }; } exports.partition = partition; function skip(n) { return async function* (xs) { let i = 0; for await (const x of xs) { if (++i <= n) continue; yield x; } }; } exports.skip = skip; function take(n) { return async function* (xs) { let i = 0; for await (const x of xs) { if (++i > n) break; yield x; } }; } exports.take = take; // TODO: rename? function partitionAt(n) { return function (xs) { const [xs1, xs2] = common_1.asyncTee(xs); return [take(n)(xs1), skip(n)(xs2)]; }; } exports.partitionAt = partitionAt; exports.splitAt = partitionAt; function skipWhile(f) { return async function* (xs) { const it = common_1.forAwaitableIterator(xs); let first; for await (const x of it) { first = x; if (!f(x)) break; } yield first; for await (const x of it) yield x; }; } exports.skipWhile = skipWhile; function takeWhile(f) { return async function* (xs) { for await (const x of xs) { if (f(x)) yield x; else break; } }; } exports.takeWhile = takeWhile; function partitionWhile(f) { return function (xs) { const [xs1, xs2] = common_1.asyncTee(xs); return [takeWhile(f)(xs1), skipWhile(f)(xs2)]; }; } exports.partitionWhile = partitionWhile; function find(p) { return async function (xs) { for await (const x of xs) { if (p(x)) return x; } return null; }; } exports.find = find; function findIndex(p) { return async function (xs) { let i = 0; for await (const x of xs) { if (p(x)) return i; i++; } return -1; }; } exports.findIndex = findIndex; function pluck(key) { return async function* (xs) { for await (const x of xs) yield x[key]; }; } exports.pluck = pluck; // like pluck, but accepts an iterable of keys function select(keys) { return async function* (xs) { for await (const x of xs) { let r = x; for (const k of keys) { r = r != null ? r[k] : undefined; } yield r; } }; } exports.select = select; function unzip2() { return function (xs) { const [xs1, xs2] = common_1.asyncTee(xs); return [pluck(0)(xs1), pluck(1)(xs2)]; }; } exports.unzip2 = unzip2; function unzip3() { return function (xs) { const [xs1, xs2, xs3] = common_1.asyncTeeN(xs, 3); return [pluck(0)(xs1), pluck(1)(xs2), pluck(2)(xs3)]; }; } exports.unzip3 = unzip3; function unzip(n = 2) { return function (xs) { const xss = common_1.asyncTeeN(xs, n); return xss.map((xs, i) => pluck(i)(xs)); }; } exports.unzip = unzip; function groupBy(f) { return async function (xs) { const res = new Map(); for await (const x of xs) { const key = f(x); if (!res.has(key)) res.set(key, []); res.get(key).push(x); } return res; }; } exports.groupBy = groupBy; function groupByKey(key) { return groupBy((x) => x[key]); } exports.groupByKey = groupByKey; function mapKeys(f) { return async function* (xs) { for await (const [k, v] of xs) yield [f(k), v]; }; } exports.mapKeys = mapKeys; function mapValues(f) { return async function* (xs) { for await (const [k, v] of xs) yield [k, f(v)]; }; } exports.mapValues = mapValues; function pairwise() { return async function* (xs) { const it = common_1.forAwaitableIterator(xs); let prev = (await it.next()).value; for await (const x of it) { yield [prev, x]; prev = x; } }; } exports.pairwise = pairwise; function length() { return async function (xs) { let c = 0; for await (const _ of xs) c++; return c; }; } exports.length = length; function min() { return async function (xs) { let res = Number.POSITIVE_INFINITY; for await (const x of xs) { if (x < res) res = x; } return res; }; } exports.min = min; function max() { return async function (xs) { let res = Number.NEGATIVE_INFINITY; for await (const x of xs) { if (x > res) res = x; } return res; }; } exports.max = max; function minMax() { return async function (xs) { let min = Number.POSITIVE_INFINITY; let max = Number.NEGATIVE_INFINITY; for await (const x of xs) { if (x < min) min = x; if (x > max) max = x; } return [min, max]; }; } exports.minMax = minMax; function minBy(cf) { return async function (xs) { const it = common_1.forAwaitableIterator(xs); const { done, value } = await it.next(); if (done) return null; let res = value; for await (const x of it) if (cf(x, res) < 0) res = x; return res; }; } exports.minBy = minBy; function maxBy(cf) { return async function (xs) { const it = common_1.forAwaitableIterator(xs); const { done, value } = await it.next(); if (done) return null; let res = value; for await (const x of it) if (cf(x, res) > 0) res = x; return res; }; } exports.maxBy = maxBy; function minMaxBy(cf) { return async function (xs) { const it = common_1.forAwaitableIterator(xs); const { done, value } = await it.next(); if (done) return [null, null]; let min = value; let max = value; for await (const x of it) { if (cf(x, min) < 0) min = x; if (cf(x, max) > 0) max = x; } return [min, max]; }; } exports.minMaxBy = minMaxBy; function minByScan(cf) { return async function* (xs) { const it = common_1.forAwaitableIterator(xs); const { done, value } = await it.next(); if (done) yield null; let res = value; for await (const x of it) { if (cf(x, res) < 0) res = x; yield res; } }; } exports.minByScan = minByScan; function maxByScan(cf) { return async function* (xs) { const it = common_1.forAwaitableIterator(xs); const { done, value } = await it.next(); if (done) yield null; let res = value; for await (const x of it) { if (cf(x, res) > 0) res = x; yield res; } }; } exports.maxByScan = maxByScan; function sum(zero = 0) { return async function (xs) { let res = zero; for await (const x of xs) res += x; return res; }; } exports.sum = sum; function replaceWhen(pf, ys) { return async function* (xs) { for await (const [x, y] of zip2(xs, ys)) { if (!pf(x)) yield x; else yield y; } }; } exports.replaceWhen = replaceWhen; function grouped(n, step = n) { return async function* (xs) { let group = []; for await (const x of xs) { group.push(x); if (group.length === n) { yield [...group]; for (let i = 0; i < step; i++) group.shift(); } } }; } exports.grouped = grouped; function startWith(...as) { return async function* (xs) { for await (const a of as) yield a; for await (const x of xs) yield x; }; } exports.startWith = startWith; function endWith(...zs) { return async function* (xs) { for await (const x of xs) yield x; for await (const z of zs) yield z; }; } exports.endWith = endWith; function sort(cf) { return async function* (xs) { let arr = []; for await (const x of xs) arr.push(x); for (const x of arr.sort(cf)) yield x; }; } exports.sort = sort; function sortScan(cf) { return async function* (xs) { let arr = []; for await (const x of xs) { arr.push(x); yield [...arr.sort(cf)]; } }; } exports.sortScan = sortScan; function flatMap(f) { return async function* (xss) { for await (const xs of xss) for await (const x of xs) yield await f(x); }; } exports.flatMap = flatMap; function distinctUntilChanged(comp = (a, b) => a === b) { return async function* (xs) { const it = common_1.forAwaitableIterator(xs); let { done, value: initial } = await it.next(); if (done) return; yield initial; for await (const x of it) if (!comp(x, initial)) yield (initial = x); }; } exports.distinctUntilChanged = distinctUntilChanged; function unique(comp = (a, b) => a === b) { return async function* (xs) { const arr = []; for await (const x of xs) arr.push(x); const unq = arr.filter((x, i, self) => self.findIndex(y => comp(x, y)) === i); for (const u of unq) yield u; }; } exports.unique = unique; function uniqueSorted(comp = (a, b) => a === b) { return async function* (xs) { const arr = []; for await (const x of xs) arr.push(x); arr.sort(); for await (const x of distinctUntilChanged(comp)(arr)) yield x; }; } exports.uniqueSorted = uniqueSorted; // CONSTRUCTORS async function* range(start = 0, end = Number.MAX_SAFE_INTEGER, step = 1) { for (let i = start; end > start ? i < end : i > end; i += step) yield i; } exports.range = range; // TODO: rename to `entries`? async function* enumerate(xs) { let i = 0; for await (const x of xs) yield [i++, x]; } exports.enumerate = enumerate; async function* concat(...xss) { for (const xs of xss) for await (const x of xs) yield x; } exports.concat = concat; exports.chain = concat; async function* zip2(xs, ys) { const xit = common_1.forAwaitableIterator(xs); const yit = common_1.forAwaitableIterator(ys); while (true) { const [xr, yr] = await Promise.all([xit.next(), yit.next()]); if (xr.done || yr.done) break; yield [xr.value, yr.value]; } } exports.zip2 = zip2; async function* zip3(xs, ys, zs) { const xit = common_1.forAwaitableIterator(xs); const yit = common_1.forAwaitableIterator(ys); const zit = common_1.forAwaitableIterator(zs); while (true) { const [xr, yr, zr] = await Promise.all([xit.next(), yit.next(), zit.next()]); if (xr.done || yr.done || zr.done) break; yield [xr.value, yr.value, zr.value]; } } exports.zip3 = zip3; async function* zip(...xss) { const its = xss.map(common_1.forAwaitableIterator); while (true) { const rs = await Promise.all(its.map(it => it.next())); if (rs.some(r => r.done)) break; yield rs.map(r => r.value); } } exports.zip = zip; // TODO: rename? Is this how regular zip commonly works? async function* zipOuter(...xss) { const its = xss.map(common_1.forAwaitableIterator); while (true) { const rs = await Promise.all(its.map(it => it.next())); if (rs.every(r => r.done)) break; yield rs.map(r => r.value); } } exports.zipOuter = zipOuter; async function* product2(as, bs) { // if (as === bs) [as, bs] = asyncTee(as); let bs2; for await (const a of as) { [bs, bs2] = common_1.asyncTee(bs); for await (const b of bs2) { yield [a, b]; } } } exports.product2 = product2; async function* product3(as, bs, cs) { // if (as === bs) [as, bs] = asyncTee(as); // if (as === cs) [as, cs] = asyncTee(as); // if (bs === cs) [bs, cs] = asyncTee(bs); let bs2; let cs2; for await (const a of as) { [bs, bs2] = common_1.asyncTee(bs); for await (const b of bs2) { [cs, cs2] = common_1.asyncTee(cs); for await (const c of cs2) { yield [a, b, c]; } } } } exports.product3 = product3; // TODO: generalize to n parameters async function* product(xs, ...xss) { throw new Error('Not implemented'); } exports.product = product; // TODO: other name (look at python itertools?) // TODO: fix implementation async function* combinations2(xs) { let [as, bs] = common_1.asyncTee(xs); let bs2; let i = 1; for await (const a of as) { [bs, bs2] = common_1.asyncTee(bs); for await (const b of skip(i++)(bs2)) { yield [a, b]; } } } exports.combinations2 = combinations2; async function* combinations3(xs) { throw Error('Not implemented'); // let [as, bs, cs] = asyncTeeN(xs, 3); // let bs2: ForOfAwaitable<X>; // let cs2: ForOfAwaitable<X>; // let i = 1; // let j = 2; // for await (const a of as) { // [bs, bs2] = asyncTee(bs); // for await (const b of skip<X>(i++)(bs2)) { // [cs, cs2] = asyncTee(cs); // for await (const c of skip<X>(j++)(cs2)) { // yield [a, b, c]; // } // } // } } exports.combinations3 = combinations3; async function* combinations(xs, r = 2) { throw Error('Not implemented'); } exports.combinations = combinations; async function* combinationsWithReplacement2(xs) { let [as, bs] = common_1.asyncTee(xs); let bs2; let i = 0; for await (const a of as) { [bs, bs2] = common_1.asyncTee(bs); for await (const b of skip(i++)(bs2)) { yield [a, b]; } } } exports.combinationsWithReplacement2 = combinationsWithReplacement2; async function* combinationsWithReplacement3(xs) { throw Error('Not implemented'); // let [as, bs, cs] = asyncTeeN(xs, 3); // let bs2: ForOfAwaitable<X>; // let cs2: ForOfAwaitable<X>; // let i = 0; // let j = 0; // for await (const a of as) { // [bs, bs2] = asyncTee(bs); // for await (const b of skip<X>(i++)(bs2)) { // [cs, cs2] = asyncTee(cs); // for await (const c of skip<X>(j++)(cs2)) { // yield [a, b, c]; // } // } // } } exports.combinationsWithReplacement3 = combinationsWithReplacement3; async function* combinationsWithReplacement(xs, r = 2) { throw Error('Not implemented'); } exports.combinationsWithReplacement = combinationsWithReplacement; async function* permutations2(xs) { throw Error('Not implemented'); } exports.permutations2 = permutations2; async function* permutations3(xs) { throw Error('Not implemented'); } exports.permutations3 = permutations3; async function* permutations(xs, r = 2) { throw Error('Not implemented'); } exports.permutations = permutations; async function* constantly(value) { while (true) yield value; } exports.constantly = constantly; async function* cycle(xs) { let xs2; while (true) { [xs, xs2] = common_1.asyncTee(xs); for await (const x of xs2) yield x; } } exports.cycle = cycle; async function* repeat(xs, n) { let xs2; for (let i = 0; i < n; i++) { [xs, xs2] = common_1.asyncTee(xs); for await (const x of xs2) yield x; } } exports.repeat = repeat; async function* interleave2(xs, ys) { const itx = common_1.forAwaitableIterator(xs); const ity = common_1.forAwaitableIterator(ys); while (true) { const rx = await itx.next(); if (rx.done) break; else yield rx.value; const ry = await ity.next(); if (ry.done) break; else yield ry.value; } } exports.interleave2 = interleave2; async function* interleave3(xs, ys, zs) { const itx = common_1.forAwaitableIterator(xs); const ity = common_1.forAwaitableIterator(ys); const itz = common_1.forAwaitableIterator(zs); while (true) { const rx = await itx.next(); if (rx.done) break; else yield rx.value; const ry = await ity.next(); if (ry.done) break; else yield ry.value; const rz = await itz.next(); if (rz.done) break; else yield rz.value; } } exports.interleave3 = interleave3; async function* interleave(...xss) { const its = xss.map(common_1.forAwaitableIterator); // Throwback to the 90s outerloop: while (true) { for (const it of its) { const { done, value } = await it.next(); // Yup, this just happened if (done) break outerloop; else yield value; } } } exports.interleave = interleave; async function pipe(x, ...fs) { let res = await x; for (const f of fs) res = await f(res); return res; } exports.pipe = pipe; //# sourceMappingURL=async-iter.js.map