ts-tco
Version:
Utility for flattening tail recursion in TypeScript
49 lines (48 loc) • 1.42 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.tco = exports.TcoThen = exports.Tco = exports.executeTco = void 0;
exports.executeTco = function (v) {
var _a;
var value = v;
var maps = [];
while (value instanceof Tco || value instanceof TcoThen || maps.length)
if (value instanceof TcoThen) {
maps.unshift(value.func);
value = value.from;
}
else if (value instanceof Tco) {
value = value.func();
}
else if (maps.length) {
value = (_a = maps.shift()) === null || _a === void 0 ? void 0 : _a(value);
}
return value;
};
var Tco = /** @class */ (function () {
function Tco(func) {
this.func = func;
}
Tco.prototype.execute = function () {
return exports.executeTco(this);
};
Tco.prototype.then = function (func) {
return new TcoThen(this, func);
};
return Tco;
}());
exports.Tco = Tco;
var TcoThen = /** @class */ (function () {
function TcoThen(from, func) {
this.from = from;
this.func = func;
}
TcoThen.prototype.execute = function () {
return exports.executeTco(this);
};
TcoThen.prototype.then = function (func) {
return new TcoThen(this, func);
};
return TcoThen;
}());
exports.TcoThen = TcoThen;
exports.tco = function (f) { return new Tco(f); };