lilit
Version:
[![Build status][tr-svg]][travis]
759 lines • 18.1 kB
JavaScript
;
// THIS FILE IS AUTO-GENEREATED! DO NOT MODIFY DIRECTLY!
//
// To edit this file, edit 'async-iter.js' and run 'npm run build:pre'.
Object.defineProperty(exports, "__esModule", { value: true });
const common_1 = require("./common");
// OPERATORS
function map(f) {
return function* (xs) {
for (const x of xs)
yield f(x);
};
}
exports.map = map;
function tap(f) {
return function* (xs) {
for (const x of xs) {
f(x);
yield x;
}
};
}
exports.tap = tap;
exports.inspect = tap;
function forEach(f) {
return function (xs) {
for (const x of xs)
f(x);
};
}
exports.forEach = forEach;
exports.subscribe = forEach;
function reduce(f, init) {
return function (xs) {
let res = init;
for (const x of xs) {
res = f(res, x);
}
return res;
};
}
exports.reduce = reduce;
function scan(f, init) {
return function* (xs) {
let res = init;
for (const x of xs) {
res = f(res, x);
yield res;
}
};
}
exports.scan = scan;
exports.reducutions = scan;
function some(p) {
return function (xs) {
for (const x of xs) {
if (p(x))
return true;
}
return false;
};
}
exports.some = some;
function every(p) {
return function (xs) {
for (const x of xs) {
if (!p(x))
return false;
}
return true;
};
}
exports.every = every;
function filter(p) {
return function* (xs) {
for (const x of xs) {
if (p(x))
yield x;
}
};
}
exports.filter = filter;
function partition(p) {
return function (xs) {
const [xs1, xs2] = common_1.tee(xs);
return [filter(p)(xs1), filter((x) => !p(x))(xs2)];
};
}
exports.partition = partition;
function skip(n) {
return function* (xs) {
let i = 0;
for (const x of xs) {
if (++i <= n)
continue;
yield x;
}
};
}
exports.skip = skip;
function take(n) {
return function* (xs) {
let i = 0;
for (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.tee(xs);
return [take(n)(xs1), skip(n)(xs2)];
};
}
exports.partitionAt = partitionAt;
exports.splitAt = partitionAt;
function skipWhile(f) {
return function* (xs) {
const it = common_1.iterator(xs);
let first;
for (const x of it) {
first = x;
if (!f(x))
break;
}
yield first;
for (const x of it)
yield x;
};
}
exports.skipWhile = skipWhile;
function takeWhile(f) {
return function* (xs) {
for (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.tee(xs);
return [takeWhile(f)(xs1), skipWhile(f)(xs2)];
};
}
exports.partitionWhile = partitionWhile;
function find(p) {
return function (xs) {
for (const x of xs) {
if (p(x))
return x;
}
return null;
};
}
exports.find = find;
function findIndex(p) {
return function (xs) {
let i = 0;
for (const x of xs) {
if (p(x))
return i;
i++;
}
return -1;
};
}
exports.findIndex = findIndex;
function pluck(key) {
return function* (xs) {
for (const x of xs)
yield x[key];
};
}
exports.pluck = pluck;
// like pluck, but accepts an iterable of keys
function select(keys) {
return function* (xs) {
for (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.tee(xs);
return [pluck(0)(xs1), pluck(1)(xs2)];
};
}
exports.unzip2 = unzip2;
function unzip3() {
return function (xs) {
const [xs1, xs2, xs3] = common_1.teeN(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.teeN(xs, n);
return xss.map((xs, i) => pluck(i)(xs));
};
}
exports.unzip = unzip;
function groupBy(f) {
return function (xs) {
const res = new Map();
for (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 function* (xs) {
for (const [k, v] of xs)
yield [f(k), v];
};
}
exports.mapKeys = mapKeys;
function mapValues(f) {
return function* (xs) {
for (const [k, v] of xs)
yield [k, f(v)];
};
}
exports.mapValues = mapValues;
function pairwise() {
return function* (xs) {
const it = common_1.iterator(xs);
let prev = (it.next()).value;
for (const x of it) {
yield [prev, x];
prev = x;
}
};
}
exports.pairwise = pairwise;
function length() {
return function (xs) {
let c = 0;
for (const _ of xs)
c++;
return c;
};
}
exports.length = length;
function min() {
return function (xs) {
let res = Number.POSITIVE_INFINITY;
for (const x of xs) {
if (x < res)
res = x;
}
return res;
};
}
exports.min = min;
function max() {
return function (xs) {
let res = Number.NEGATIVE_INFINITY;
for (const x of xs) {
if (x > res)
res = x;
}
return res;
};
}
exports.max = max;
function minMax() {
return function (xs) {
let min = Number.POSITIVE_INFINITY;
let max = Number.NEGATIVE_INFINITY;
for (const x of xs) {
if (x < min)
min = x;
if (x > max)
max = x;
}
return [min, max];
};
}
exports.minMax = minMax;
function minBy(cf) {
return function (xs) {
const it = common_1.iterator(xs);
const { done, value } = it.next();
if (done)
return null;
let res = value;
for (const x of it)
if (cf(x, res) < 0)
res = x;
return res;
};
}
exports.minBy = minBy;
function maxBy(cf) {
return function (xs) {
const it = common_1.iterator(xs);
const { done, value } = it.next();
if (done)
return null;
let res = value;
for (const x of it)
if (cf(x, res) > 0)
res = x;
return res;
};
}
exports.maxBy = maxBy;
function minMaxBy(cf) {
return function (xs) {
const it = common_1.iterator(xs);
const { done, value } = it.next();
if (done)
return [null, null];
let min = value;
let max = value;
for (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 function* (xs) {
const it = common_1.iterator(xs);
const { done, value } = it.next();
if (done)
yield null;
let res = value;
for (const x of it) {
if (cf(x, res) < 0)
res = x;
yield res;
}
};
}
exports.minByScan = minByScan;
function maxByScan(cf) {
return function* (xs) {
const it = common_1.iterator(xs);
const { done, value } = it.next();
if (done)
yield null;
let res = value;
for (const x of it) {
if (cf(x, res) > 0)
res = x;
yield res;
}
};
}
exports.maxByScan = maxByScan;
function sum(zero = 0) {
return function (xs) {
let res = zero;
for (const x of xs)
res += x;
return res;
};
}
exports.sum = sum;
function replaceWhen(pf, ys) {
return function* (xs) {
for (const [x, y] of zip2(xs, ys)) {
if (!pf(x))
yield x;
else
yield y;
}
};
}
exports.replaceWhen = replaceWhen;
function grouped(n, step = n) {
return function* (xs) {
let group = [];
for (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 function* (xs) {
for (const a of as)
yield a;
for (const x of xs)
yield x;
};
}
exports.startWith = startWith;
function endWith(...zs) {
return function* (xs) {
for (const x of xs)
yield x;
for (const z of zs)
yield z;
};
}
exports.endWith = endWith;
function sort(cf) {
return function* (xs) {
let arr = [];
for (const x of xs)
arr.push(x);
for (const x of arr.sort(cf))
yield x;
};
}
exports.sort = sort;
function sortScan(cf) {
return function* (xs) {
let arr = [];
for (const x of xs) {
arr.push(x);
yield [...arr.sort(cf)];
}
};
}
exports.sortScan = sortScan;
function flatMap(f) {
return function* (xss) {
for (const xs of xss)
for (const x of xs)
yield f(x);
};
}
exports.flatMap = flatMap;
function distinctUntilChanged(comp = (a, b) => a === b) {
return function* (xs) {
const it = common_1.iterator(xs);
let { done, value: initial } = it.next();
if (done)
return;
yield initial;
for (const x of it)
if (!comp(x, initial))
yield (initial = x);
};
}
exports.distinctUntilChanged = distinctUntilChanged;
function unique(comp = (a, b) => a === b) {
return function* (xs) {
const arr = [];
for (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 function* (xs) {
const arr = [];
for (const x of xs)
arr.push(x);
arr.sort();
for (const x of distinctUntilChanged(comp)(arr))
yield x;
};
}
exports.uniqueSorted = uniqueSorted;
// CONSTRUCTORS
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`?
function* enumerate(xs) {
let i = 0;
for (const x of xs)
yield [i++, x];
}
exports.enumerate = enumerate;
function* concat(...xss) {
for (const xs of xss)
for (const x of xs)
yield x;
}
exports.concat = concat;
exports.chain = concat;
function* zip2(xs, ys) {
const xit = common_1.iterator(xs);
const yit = common_1.iterator(ys);
while (true) {
const [xr, yr] = [xit.next(), yit.next()];
if (xr.done || yr.done)
break;
yield [xr.value, yr.value];
}
}
exports.zip2 = zip2;
function* zip3(xs, ys, zs) {
const xit = common_1.iterator(xs);
const yit = common_1.iterator(ys);
const zit = common_1.iterator(zs);
while (true) {
const [xr, yr, zr] = [xit.next(), yit.next(), zit.next()];
if (xr.done || yr.done || zr.done)
break;
yield [xr.value, yr.value, zr.value];
}
}
exports.zip3 = zip3;
function* zip(...xss) {
const its = xss.map(common_1.iterator);
while (true) {
const rs = 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?
function* zipOuter(...xss) {
const its = xss.map(common_1.iterator);
while (true) {
const rs = its.map(it => it.next());
if (rs.every(r => r.done))
break;
yield rs.map(r => r.value);
}
}
exports.zipOuter = zipOuter;
function* product2(as, bs) {
// if (as === bs) [as, bs] = tee(as);
let bs2;
for (const a of as) {
[bs, bs2] = common_1.tee(bs);
for (const b of bs2) {
yield [a, b];
}
}
}
exports.product2 = product2;
function* product3(as, bs, cs) {
// if (as === bs) [as, bs] = tee(as);
// if (as === cs) [as, cs] = tee(as);
// if (bs === cs) [bs, cs] = tee(bs);
let bs2;
let cs2;
for (const a of as) {
[bs, bs2] = common_1.tee(bs);
for (const b of bs2) {
[cs, cs2] = common_1.tee(cs);
for (const c of cs2) {
yield [a, b, c];
}
}
}
}
exports.product3 = product3;
// TODO: generalize to n parameters
function* product(xs, ...xss) {
throw new Error('Not implemented');
}
exports.product = product;
// TODO: other name (look at python itertools?)
// TODO: fix implementation
function* combinations2(xs) {
let [as, bs] = common_1.tee(xs);
let bs2;
let i = 1;
for (const a of as) {
[bs, bs2] = common_1.tee(bs);
for (const b of skip(i++)(bs2)) {
yield [a, b];
}
}
}
exports.combinations2 = combinations2;
function* combinations3(xs) {
throw Error('Not implemented');
// let [as, bs, cs] = teeN(xs, 3);
// let bs2: Iterable<X>;
// let cs2: Iterable<X>;
// let i = 1;
// let j = 2;
// for (const a of as) {
// [bs, bs2] = tee(bs);
// for (const b of skip<X>(i++)(bs2)) {
// [cs, cs2] = tee(cs);
// for (const c of skip<X>(j++)(cs2)) {
// yield [a, b, c];
// }
// }
// }
}
exports.combinations3 = combinations3;
function* combinations(xs, r = 2) {
throw Error('Not implemented');
}
exports.combinations = combinations;
function* combinationsWithReplacement2(xs) {
let [as, bs] = common_1.tee(xs);
let bs2;
let i = 0;
for (const a of as) {
[bs, bs2] = common_1.tee(bs);
for (const b of skip(i++)(bs2)) {
yield [a, b];
}
}
}
exports.combinationsWithReplacement2 = combinationsWithReplacement2;
function* combinationsWithReplacement3(xs) {
throw Error('Not implemented');
// let [as, bs, cs] = teeN(xs, 3);
// let bs2: Iterable<X>;
// let cs2: Iterable<X>;
// let i = 0;
// let j = 0;
// for (const a of as) {
// [bs, bs2] = tee(bs);
// for (const b of skip<X>(i++)(bs2)) {
// [cs, cs2] = tee(cs);
// for (const c of skip<X>(j++)(cs2)) {
// yield [a, b, c];
// }
// }
// }
}
exports.combinationsWithReplacement3 = combinationsWithReplacement3;
function* combinationsWithReplacement(xs, r = 2) {
throw Error('Not implemented');
}
exports.combinationsWithReplacement = combinationsWithReplacement;
function* permutations2(xs) {
throw Error('Not implemented');
}
exports.permutations2 = permutations2;
function* permutations3(xs) {
throw Error('Not implemented');
}
exports.permutations3 = permutations3;
function* permutations(xs, r = 2) {
throw Error('Not implemented');
}
exports.permutations = permutations;
function* constantly(value) {
while (true)
yield value;
}
exports.constantly = constantly;
function* cycle(xs) {
let xs2;
while (true) {
[xs, xs2] = common_1.tee(xs);
for (const x of xs2)
yield x;
}
}
exports.cycle = cycle;
function* repeat(xs, n) {
let xs2;
for (let i = 0; i < n; i++) {
[xs, xs2] = common_1.tee(xs);
for (const x of xs2)
yield x;
}
}
exports.repeat = repeat;
function* interleave2(xs, ys) {
const itx = common_1.iterator(xs);
const ity = common_1.iterator(ys);
while (true) {
const rx = itx.next();
if (rx.done)
break;
else
yield rx.value;
const ry = ity.next();
if (ry.done)
break;
else
yield ry.value;
}
}
exports.interleave2 = interleave2;
function* interleave3(xs, ys, zs) {
const itx = common_1.iterator(xs);
const ity = common_1.iterator(ys);
const itz = common_1.iterator(zs);
while (true) {
const rx = itx.next();
if (rx.done)
break;
else
yield rx.value;
const ry = ity.next();
if (ry.done)
break;
else
yield ry.value;
const rz = itz.next();
if (rz.done)
break;
else
yield rz.value;
}
}
exports.interleave3 = interleave3;
function* interleave(...xss) {
const its = xss.map(common_1.iterator);
// Throwback to the 90s
outerloop: while (true) {
for (const it of its) {
const { done, value } = it.next();
// Yup, this just happened
if (done)
break outerloop;
else
yield value;
}
}
}
exports.interleave = interleave;
function pipe(x, ...fs) {
let res = x;
for (const f of fs)
res = f(res);
return res;
}
exports.pipe = pipe;
//# sourceMappingURL=iter.js.map