UNPKG

lazzy.ts

Version:

Fast and lightweight library for lazy operations with iterable objects.

85 lines 3.9 kB
"use strict"; 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 = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i; function verb(n) { if (g[n]) i[n] = function (v) { return new Promise(function (a, b) { q.push([n, v, a, b]) > 1 || resume(n, v); }); }; } 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]); } }; Object.defineProperty(exports, "__esModule", { value: true }); exports.lazyGroupByAsync = exports.lazyGroupBy = void 0; const chain_1 = require("../chain"); const channel_1 = require("../csp/channel"); function* lazyGroupBy(iterator, keySelector, elementSelector, resultSelector) { const groups = new Map(); let x = iterator.next(); while (x.done !== true) { const key = keySelector(x.value); const element = elementSelector(x.value); if (!groups.has(key)) { const channel = new channel_1.Channel(); yield resultSelector(key, new chain_1.ChainAsync(function () { return __asyncGenerator(this, arguments, function* () { while (!channel.isClosed()) { const value = yield __await(channel.take()); if (channel_1.isClosed(value)) { break; } yield yield __await(value); } }); }())); groups.set(key, channel); } const channel = groups.get(key); channel.put(element); x = iterator.next(); } for (const [, channel] of groups) { channel.close(); } return x.value; } exports.lazyGroupBy = lazyGroupBy; /** * This is temporary solution. Now it is not effective enough... * @todo We should refactor this function to work even more lazy! */ function lazyGroupByAsync(iterator, keySelector, elementSelector, resultSelector) { return __asyncGenerator(this, arguments, function* lazyGroupByAsync_1() { const groups = new Map(); let x = yield __await(iterator.next()); while (x.done !== true) { const key = keySelector(x.value); const element = elementSelector(x.value); if (!groups.has(key)) { groups.set(key, new channel_1.Channel()); } const channel = groups.get(key); channel.put(element); x = yield __await(iterator.next()); } for (const [key, channel] of groups) { channel.close(); yield yield __await(resultSelector(key, new chain_1.ChainAsync(function () { return __asyncGenerator(this, arguments, function* () { while (!channel.isClosed()) { const value = yield __await(channel.take()); if (channel_1.isClosed(value)) { break; } yield yield __await(value); } }); }()))); } return yield __await(x.value); }); } exports.lazyGroupByAsync = lazyGroupByAsync; //# sourceMappingURL=lazyGroupBy.js.map