lazzy.ts
Version:
Fast and lightweight library for lazy operations with iterable objects.
118 lines • 5.05 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
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.lazyPartitionAsync = exports.lazyPartition = void 0;
const chain_1 = require("../chain");
const channel_1 = require("../csp/channel");
function* lazyPartition(iterator, predicate) {
const positive = new channel_1.Channel();
const negative = new channel_1.Channel();
yield new chain_1.ChainAsync((function () {
return __asyncGenerator(this, arguments, function* () {
while (!positive.isClosed()) {
const value = yield __await(positive.take());
if (channel_1.isClosed(value)) {
break;
}
yield yield __await(value);
}
});
})());
yield new chain_1.ChainAsync((function () {
return __asyncGenerator(this, arguments, function* () {
while (!negative.isClosed()) {
const value = yield __await(negative.take());
if (channel_1.isClosed(value)) {
break;
}
yield yield __await(value);
}
});
})());
let index = 0;
let x = iterator.next();
while (x.done !== true) {
if (predicate(x.value, index++)) {
positive.put(x.value);
}
else {
negative.put(x.value);
}
x = iterator.next();
}
positive.close();
negative.close();
return x.value;
}
exports.lazyPartition = lazyPartition;
/**
* This is temporary solution. This is a good solution, but we should return an AsyncGenerator.
* @todo We should refactor this function. It should be an AsyncGenerator.
*/
function* lazyPartitionAsync(iterator, predicate) {
const positive = new channel_1.Channel();
const negative = new channel_1.Channel();
yield new chain_1.ChainAsync((function () {
return __asyncGenerator(this, arguments, function* () {
while (!positive.isClosed()) {
const value = yield __await(positive.take());
if (channel_1.isClosed(value)) {
break;
}
yield yield __await(value);
}
});
})());
yield new chain_1.ChainAsync((function () {
return __asyncGenerator(this, arguments, function* () {
while (!negative.isClosed()) {
const value = yield __await(negative.take());
if (channel_1.isClosed(value)) {
break;
}
yield yield __await(value);
}
});
})());
let index = 0;
function iterate(x) {
return __awaiter(this, void 0, void 0, function* () {
if (x.done !== true) {
if (yield predicate(x.value, index++)) {
positive.put(x.value);
}
else {
negative.put(x.value);
}
Promise.resolve(iterator.next()).then(iterate);
}
else {
positive.close();
negative.close();
}
});
}
Promise.resolve(iterator.next()).then(iterate);
}
exports.lazyPartitionAsync = lazyPartitionAsync;
//# sourceMappingURL=lazyPartition.js.map