branching and very basic merging of slots
This commit is contained in:
parent
614e6c0fdb
commit
3978f7f835
32 changed files with 684 additions and 420 deletions
122
scripts/versioning.js
Normal file
122
scripts/versioning.js
Normal file
|
|
@ -0,0 +1,122 @@
|
|||
import { pretty } from "../util/pretty.js";
|
||||
import { newLiteral, transform, read, getReadDependencies, verifyValue } from "../versioning/value.js";
|
||||
import { merge, merge2, newSlot, overwrite } from "../versioning/slot.js";
|
||||
import createRBTree from "functional-red-black-tree";
|
||||
import { add, emptySet, RBTreeWrapper } from "../structures/set.js";
|
||||
|
||||
const inc = x => x + 1;
|
||||
|
||||
|
||||
console.log("##############");
|
||||
console.log("## Counting ##");
|
||||
console.log("##############");
|
||||
|
||||
const counterOneSlot = newSlot(Symbol('counter'))(newLiteral(1));
|
||||
const valueOne = read(counterOneSlot);
|
||||
console.log(pretty({valueOne}));
|
||||
const onePlusOne = transform(valueOne)(newLiteral(inc));
|
||||
console.log(pretty({onePlusOne}));
|
||||
const onePlusOnePlusOne = transform(onePlusOne)(newLiteral(inc));
|
||||
console.log(pretty({onePlusOnePlusOne}));
|
||||
|
||||
verifyValue(valueOne);
|
||||
verifyValue(onePlusOne);
|
||||
verifyValue(onePlusOnePlusOne);
|
||||
|
||||
|
||||
|
||||
console.log("#############");
|
||||
console.log("## Summing ##");
|
||||
console.log("#############");
|
||||
|
||||
const priceSlot = newSlot(Symbol('price'))(newLiteral(20.66));
|
||||
const taxSlot = newSlot(Symbol('tax'))(newLiteral(4.34));
|
||||
|
||||
const total =
|
||||
transform(read(priceSlot))(
|
||||
transform(read(taxSlot))(
|
||||
newLiteral(tax => price => price + tax)));
|
||||
|
||||
console.log(pretty({total}))
|
||||
|
||||
const totalPlusOne = transform(total)(newLiteral(inc));
|
||||
|
||||
console.log(pretty({totalPlusOne}));
|
||||
|
||||
verifyValue(totalPlusOne);
|
||||
|
||||
console.log("getReadDependencies(totalPlusOne):", getReadDependencies(totalPlusOne));
|
||||
|
||||
|
||||
|
||||
console.log("###############");
|
||||
console.log("## Branching ##");
|
||||
console.log("###############");
|
||||
|
||||
const fiveSlot = newSlot(Symbol('counter'))(newLiteral(5));
|
||||
const sixSlot = overwrite(fiveSlot)(newLiteral(6));
|
||||
const sevenSlot = overwrite(fiveSlot)(newLiteral(7));
|
||||
const eightSlot = overwrite(fiveSlot)(newLiteral(8));
|
||||
|
||||
const numCompare = x => y => {
|
||||
if (typeof(x) !== 'number' || typeof(y) !== 'number') {
|
||||
throw new Error(`was only meant to compare numbers! got ${x} and ${y}`);
|
||||
}
|
||||
return (x < y) ? -1 : (x > y) ? 1 : 0;
|
||||
};
|
||||
const intMerge = merge(numCompare);
|
||||
const intMerge2 = merge2(numCompare);
|
||||
|
||||
const sixSevenSlot = intMerge(sixSlot)(sevenSlot);
|
||||
const sevenEightSlot = intMerge(sevenSlot)(eightSlot);
|
||||
|
||||
// console.log(compareSlots(intCompare)(fiveSlot)(fiveSlot));
|
||||
// console.log(compareSlots(intCompare)(sixSlot)(sixSlot));
|
||||
// console.log(compareSlots(intCompare)(fiveSlot)(sixSlot));
|
||||
// console.log(compareSlots(intCompare)(sixSlot)(fiveSlot));
|
||||
|
||||
const sixSevenEightSlot = intMerge2(sixSevenSlot)(sevenEightSlot);
|
||||
|
||||
console.log(pretty({sixSevenEightSlot}));
|
||||
|
||||
// console.log("########################");
|
||||
// console.log("## Heterogeneous data ##");
|
||||
// console.log("########################");
|
||||
|
||||
// const strCompare = x => y => {
|
||||
// if (typeof(x) !== 'string' || typeof(y) !== 'string') {
|
||||
// throw new Error(`was only meant to compare strings! got ${x} and ${y}`);
|
||||
// }
|
||||
// return (x < y) ? -1 : (x > y) ? 1 : 0;
|
||||
// };
|
||||
|
||||
// // Slot<Int>
|
||||
// const numberOfSheepSlot = newSlot(Symbol('numberOfSheep'))(newLiteral(5));
|
||||
// const alternativeNumberOfSheepSlot = newSlot(Symbol('alternativeNumberOfSheep'))(newLiteral(6));
|
||||
// // Slot<String>
|
||||
// const labelSlot = newSlot(Symbol('label'))(newLiteral("number of sheep"));
|
||||
|
||||
// const combineFn = newLiteral(label => numberOfSheep => `${label}: ${numberOfSheep}`)
|
||||
|
||||
// // Slot<String>
|
||||
// const labelAndValueSlotA = overwrite(labelSlot)(
|
||||
// transform(read(numberOfSheepSlot))(
|
||||
// transform(read(labelSlot))(combineFn)));
|
||||
|
||||
// const labelAndValueSlotB = overwrite(labelSlot)(
|
||||
// transform(read(alternativeNumberOfSheepSlot))(
|
||||
// transform(read(labelSlot))(combineFn)));
|
||||
|
||||
// console.log(
|
||||
// add(add(emptySet(compareSlots(strCompare)))(labelAndValueSlotA))(labelAndValueSlotB)
|
||||
// );
|
||||
|
||||
// merge()(labelSlot)(labelAndValueSlot)
|
||||
|
||||
|
||||
console.log("#############")
|
||||
console.log("## RB Tree ##")
|
||||
console.log("#############")
|
||||
|
||||
// just a small experiment
|
||||
console.log(new RBTreeWrapper(createRBTree().insert(1).insert(1).insert(2)));
|
||||
Loading…
Add table
Add a link
Reference in a new issue