UNPKG

@thi.ng/pointfree

Version:

Pointfree functional composition / Forth style stack execution engine

24 lines (23 loc) 581 B
import { isArray } from "@thi.ng/checks/is-array"; import { isFunction } from "@thi.ng/checks/is-function"; import { unwrap } from "./word.js"; const run = (prog, ctx = [[], [], {}]) => { if (isFunction(prog)) { return prog(ctx); } for (let p = isArray(prog) ? prog : [prog], n = p.length, i = 0, w; i < n; i++) { if (isFunction(w = p[i])) { ctx = w(ctx); } else { ctx[0].push(w); } } return ctx; }; const runU = (prog, ctx, n = 1) => unwrap(run(prog, ctx), n); const runE = (prog, ctx) => run(prog, ctx)[2]; export { run, runE, runU };