66 lines
1.6 KiB
JavaScript
66 lines
1.6 KiB
JavaScript
import { pretty } from "../util/pretty.js";
|
|
import { inspect } from "node:util";
|
|
|
|
// UUID -> Computation<a> -> Slot<a>
|
|
const newSlot = uuid => computation => ({
|
|
overwrites: uuid,
|
|
computation,
|
|
// depth: 1,
|
|
// [inspect.custom]: (depth, options, inspect) => options.stylize(`newSlot(${inspect(uuid)})(${inspect(computation)})`, 'special'),
|
|
});
|
|
|
|
// a -> Computation<a>
|
|
const newValue = val => ({
|
|
kind: "value",
|
|
out: val,
|
|
// [inspect.custom]: (depth, options, inspect) => options.stylize(`newValue(${val})`, 'special'),
|
|
});
|
|
|
|
// Slot<a> -> Computation<a>
|
|
const read = slot => ({
|
|
kind: "transformation",
|
|
in: slot,
|
|
fn: `${x => x}`,
|
|
out: slot.computation.out,
|
|
// [inspect.custom]: (depth, options, inspect) => options.stylize(`read(${inspect(slot)})`, 'special'),
|
|
});
|
|
|
|
// Computation<a> -> Computation<a -> b> -> Computation<b>
|
|
const transform = input => fn => {
|
|
const output = fn.out(input.out);
|
|
if (input.kind === "value") {
|
|
// our input had no read dependency, thus likewise our output does not:
|
|
return {
|
|
kind: "value",
|
|
out: output,
|
|
};
|
|
}
|
|
else {
|
|
return {
|
|
kind: "transformation",
|
|
in: input.in,
|
|
fn,
|
|
out: output,
|
|
};
|
|
}
|
|
};
|
|
|
|
|
|
|
|
const price = newSlot(Symbol('price'))(newValue(5));
|
|
const tax = newSlot(Symbol('tax'))(newValue(1));
|
|
|
|
const total =
|
|
transform(read(price))(
|
|
transform(read(tax))(
|
|
newValue(tax => price => price + tax)));
|
|
|
|
console.log(pretty({total,}));
|
|
|
|
|
|
|
|
const getReadDependencies = computation => new Set([
|
|
...(computation.kind === "value" ? [] : [computation.in, ...getReadDependencies(computation.fn)]),
|
|
]);
|
|
|
|
console.log("getReadDependencies(total):", getReadDependencies(total));
|