experiment with encoding of read- and write-dependencies
This commit is contained in:
parent
c6c49a0966
commit
248c41e99f
1 changed files with 66 additions and 0 deletions
66
scripts/versioning2.js
Normal file
66
scripts/versioning2.js
Normal file
|
|
@ -0,0 +1,66 @@
|
|||
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));
|
||||
Loading…
Add table
Add a link
Reference in a new issue