@thi.ng/pointfree
Version:
Pointfree functional composition / Forth style stack execution engine
54 lines (53 loc) • 1.15 kB
JavaScript
import { defOp1, defOp2 } from "./ops.js";
import { swap } from "./stack.js";
import { defWord } from "./word.js";
const add = defOp2((b, a) => a + b);
const mul = defOp2((b, a) => a * b);
const sub = defOp2((b, a) => a - b);
const div = defOp2((b, a) => a / b);
const oneover = defWord([1, swap, div]);
const mod = defOp2((b, a) => a % b);
const min = defOp2(Math.min);
const max = defOp2(Math.max);
const neg = defOp1((x) => -x);
const pow = defOp2((b, a) => Math.pow(a, b));
const sqrt = defOp1(Math.sqrt);
const exp = defOp1(Math.exp);
const log = defOp1(Math.log);
const sin = defOp1(Math.sin);
const cos = defOp1(Math.cos);
const tan = defOp1(Math.tan);
const tanh = defOp1(Math.tanh);
const floor = defOp1(Math.floor);
const ceil = defOp1(Math.ceil);
const hypot = defOp2(Math.hypot);
const atan2 = defOp2(Math.atan2);
const rand = (ctx) => (ctx[0].push(Math.random()), ctx);
const even = defOp1((x) => !(x & 1));
const odd = defOp1((x) => !!(x & 1));
export {
add,
atan2,
ceil,
cos,
div,
even,
exp,
floor,
hypot,
log,
max,
min,
mod,
mul,
neg,
odd,
oneover,
pow,
rand,
sin,
sqrt,
sub,
tan,
tanh
};