when chaining functions, properly carry over read-dependencies the functions
This commit is contained in:
parent
248c41e99f
commit
7a5b9d4f38
1 changed files with 46 additions and 13 deletions
|
|
@ -16,17 +16,23 @@ const newValue = val => ({
|
|||
// [inspect.custom]: (depth, options, inspect) => options.stylize(`newValue(${val})`, 'special'),
|
||||
});
|
||||
|
||||
const id = x => x;
|
||||
|
||||
// Slot<a> -> Computation<a>
|
||||
const read = slot => ({
|
||||
kind: "transformation",
|
||||
in: slot,
|
||||
fn: `${x => x}`,
|
||||
fn: {
|
||||
kind: "value",
|
||||
out: id,
|
||||
},
|
||||
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 => {
|
||||
// console.log("transform...", "input:", pretty(input), "fn:", pretty(fn));
|
||||
const output = fn.out(input.out);
|
||||
if (input.kind === "value") {
|
||||
// our input had no read dependency, thus likewise our output does not:
|
||||
|
|
@ -36,31 +42,58 @@ const transform = input => fn => {
|
|||
};
|
||||
}
|
||||
else {
|
||||
// console.log("transforming the transformation...");
|
||||
return {
|
||||
kind: "transformation",
|
||||
in: input.in,
|
||||
fn,
|
||||
// transform the transformation:
|
||||
fn: transform(input.fn)(newValue(origFn => {
|
||||
const descr = `${fn.out.name} ∘ ${origFn.name}`;
|
||||
return { [descr]: x => fn.out(origFn(x)) }[descr];
|
||||
})),
|
||||
out: output,
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
const counterOne = newSlot(Symbol('counter'))(newValue(1));
|
||||
|
||||
const valueOne = read(counterOne);
|
||||
|
||||
const price = newSlot(Symbol('price'))(newValue(5));
|
||||
const tax = newSlot(Symbol('tax'))(newValue(1));
|
||||
console.log(pretty({valueOne}));
|
||||
|
||||
const total =
|
||||
transform(read(price))(
|
||||
transform(read(tax))(
|
||||
newValue(tax => price => price + tax)));
|
||||
const inc = x => x + 1;
|
||||
|
||||
console.log(pretty({total,}));
|
||||
const onePlusOne = transform(valueOne)(newValue(inc));
|
||||
|
||||
console.log(pretty({onePlusOne}));
|
||||
|
||||
const onePlusOnePlusOne = transform(onePlusOne)(newValue(inc));
|
||||
|
||||
console.log(pretty({onePlusOnePlusOne}));
|
||||
|
||||
|
||||
|
||||
const getReadDependencies = computation => new Set([
|
||||
...(computation.kind === "value" ? [] : [computation.in, ...getReadDependencies(computation.fn)]),
|
||||
]);
|
||||
// const price = newSlot(Symbol('price'))(newValue(20.66));
|
||||
// const tax = newSlot(Symbol('tax'))(newValue(4.34));
|
||||
|
||||
console.log("getReadDependencies(total):", getReadDependencies(total));
|
||||
// console.log(pretty({price}));
|
||||
|
||||
// const total =
|
||||
// transform(read(price))(
|
||||
// transform(read(tax))(
|
||||
// newValue(tax => price => price + tax)));
|
||||
|
||||
// console.log(pretty({total}))
|
||||
|
||||
// const totalPlusOne = transform(total)(newValue(x => x + 1));
|
||||
|
||||
// console.log(pretty({totalPlusOne}));
|
||||
|
||||
|
||||
|
||||
// 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