restructure code a bit, add comparison functions for primitive types and composed types (needed to put values in sets)
This commit is contained in:
parent
3978f7f835
commit
8653bb99c6
12 changed files with 175 additions and 64 deletions
29
compare/primitives.js
Normal file
29
compare/primitives.js
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
import { Char, Double, Int } from "../primitives/types.js";
|
||||
|
||||
export const compareNumbers = 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;
|
||||
}
|
||||
|
||||
export const compareStrings = 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;
|
||||
}
|
||||
|
||||
export const compareBools = x => y => {
|
||||
if (typeof(x) !== 'boolean' || typeof(y) !== 'boolean') {
|
||||
throw new Error(`was only meant to compare booleans! got ${x} and ${y}`);
|
||||
}
|
||||
return x - y;
|
||||
};
|
||||
|
||||
export const typeToCmp = new Map([
|
||||
[Int, compareNumbers],
|
||||
[Char, compareStrings],
|
||||
[Double, compareNumbers],
|
||||
[Boolean, compareBools],
|
||||
]);
|
||||
58
compare/structures.js
Normal file
58
compare/structures.js
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
import { compareNumbers } from "./primitives.js"
|
||||
import { length as lengthLs } from "../structures/list.js";
|
||||
import { read, length as lengthSet } from "../structures/set.js";
|
||||
import { constructorProduct, getLeft, getRight } from "../structures/product.js";
|
||||
import { match } from "../structures/sum.js";
|
||||
|
||||
// (a -> a -> Int) -> [a] -> [a] -> Int
|
||||
export const compareLists = compareElems => x => y => {
|
||||
return compareNumbers(lengthLs(x))(lengthLs(y))
|
||||
|| (() => {
|
||||
for (let i=0; i<lengthLs(x); i++) {
|
||||
const elemCmp = compareElems(get(x)(i))(get(y)(i));
|
||||
if (elemCmp !== 0) {
|
||||
return elemCmp;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
})();
|
||||
};
|
||||
|
||||
// (a -> a -> Int) -> (b -> b -> Int) -> (a, b) -> (a, b) -> Int
|
||||
export const compareProducts = compareLeft => compareRight => x => y => {
|
||||
return compareLeft (getLeft (x))(getLeft (y))
|
||||
|| compareRight(getRight(x))(getRight(y));
|
||||
};
|
||||
|
||||
// (a -> a -> Int) -> (b -> b -> Int) -> (a | b) -> (a | b) -> Int
|
||||
export const compareSums = compareLeft => compareRight => x => y => {
|
||||
return match(x)(constructorProduct
|
||||
(leftValueX => match(y)(constructorProduct
|
||||
(leftValueY => compareLeft(leftValueX)(leftValueY))
|
||||
((rightValueY) => -1) // x is 'left' and y is 'right' => x < y
|
||||
))
|
||||
(rightValueX => match(y)(constructorProduct
|
||||
(leftValueY => 1) // x is 'right' and y is 'left' => x > y
|
||||
(rightValueY => compareRight(rightValueX)(rightValueY))
|
||||
))
|
||||
);
|
||||
};
|
||||
|
||||
// (a -> a -> Int) -> {a} -> {a} -> Int
|
||||
export const compareSets = compareElems => x => y => {
|
||||
return compareNumbers(lengthSet(x))(lengthSet(y))
|
||||
|| (() => {
|
||||
// sets have same size -> iterate over both sets and compare their elements pairwise
|
||||
// because of the underlying red-black tree, iteration happens in ordered fashion
|
||||
const iterate = iterX => iterY =>
|
||||
read(iterX)
|
||||
(keyX => nextX =>
|
||||
read(iterY)
|
||||
// we could also use the comparison function that is embedded in the set object,
|
||||
// but to be consistent with the other comparison-functions, we don't.
|
||||
(keyY => nextY => compareElems(keyX)(keyY) || iterate(nextX)(nextY))
|
||||
(0)) // end of set y (we'll never get here because sets are same size)
|
||||
(0) // end of set x;
|
||||
iterate(first(x))(first(y));
|
||||
})();
|
||||
};
|
||||
31
compare/versioning.js
Normal file
31
compare/versioning.js
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
export const compareSlots = compareElems => slotA => slotB => {
|
||||
if (slotA.depth < slotB.depth) {
|
||||
return -1;
|
||||
}
|
||||
if (slotB.depth < slotA.depth) {
|
||||
return 1;
|
||||
}
|
||||
return compareValues(compareElems)(slotA.value)(slotB.value);
|
||||
};
|
||||
|
||||
export const compareValues = compareElems => valA => valB => {
|
||||
if (valA.kind < valB.kind) {
|
||||
return -1;
|
||||
}
|
||||
if (valB.kind < valA.kind) {
|
||||
return 1;
|
||||
}
|
||||
if (valA.kind === "literal") {
|
||||
return compareElems(valA.out)(valB.out);
|
||||
}
|
||||
if (valA.kind === "read") {
|
||||
return compareSlots(compareElems)(valA.slot)(valB.slot);
|
||||
}
|
||||
if (valA.kind === "transformation") {
|
||||
const cmpIn = compareValues(compareElems)(valA.in)(valB.in);
|
||||
if (cmpIn !== 0) {
|
||||
return cmpIn;
|
||||
}
|
||||
return compareValues(compareElems)(valA.fn)(valB.fn);
|
||||
}
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue