trampoline-ts
Version:
A type-safe way to emulate tail-call optimization with trampolines
24 lines • 770 B
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const thunk_1 = require("./thunk");
exports.trampoline = (fn) => {
const cont = (...args) => thunk_1.toThunk(() => fn(...args));
return Object.assign((...args) => {
let result = fn(...args);
while (thunk_1.isThunk(result)) {
result = result();
}
return result;
}, { cont });
};
exports.trampolineAsync = (fn) => {
const cont = (...args) => thunk_1.toThunk(() => fn(...args));
return Object.assign(async (...args) => {
let result = await fn(...args);
while (thunk_1.isThunk(result)) {
result = await result();
}
return result;
}, { cont });
};
//# sourceMappingURL=trampoline.js.map