import { pretty } from "../util/pretty.js"; import { inspect } from "node:util"; // UUID -> Computation -> Slot const newSlot = uuid => computation => ({ overwrites: uuid, computation, // depth: 1, // [inspect.custom]: (depth, options, inspect) => options.stylize(`newSlot(${inspect(uuid)})(${inspect(computation)})`, 'special'), }); // a -> Computation const newValue = val => ({ kind: "value", out: val, // [inspect.custom]: (depth, options, inspect) => options.stylize(`newValue(${val})`, 'special'), }); // Slot -> Computation 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 -> Computation b> -> Computation 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));