reorganize directory and file structure
This commit is contained in:
parent
1d826ea8d4
commit
48390b8556
99 changed files with 1155 additions and 1629 deletions
66
lib/compare/structures.js
Normal file
66
lib/compare/structures.js
Normal file
|
|
@ -0,0 +1,66 @@
|
|||
// Total ordering of composed types
|
||||
|
||||
import { compareNumbers } from "./primitives.js"
|
||||
import { get, length as lengthLs } from "../structures/list.js";
|
||||
import { read, length as lengthSet } from "../structures/set.js";
|
||||
import { newProduct, 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 => {
|
||||
// console.log("compareSums...", x, y)
|
||||
return match(x)
|
||||
(leftValueX => match(y)
|
||||
(leftValueY => compareLeft(leftValueX)(leftValueY)) // both are left
|
||||
((_rightValueY) => {
|
||||
// console.log("x is 'left' and y is 'right' => x < y")
|
||||
return -1;
|
||||
}) // x is 'left' and y is 'right' => x < y
|
||||
)
|
||||
(rightValueX => match(y)
|
||||
(_leftValueY => {
|
||||
// console.log("x is 'right' and y is 'left' => x > y");
|
||||
return 1;
|
||||
}) // x is 'right' and y is 'left' => x > y
|
||||
(rightValueY => compareRight(rightValueX)(rightValueY)) // both are right
|
||||
);
|
||||
};
|
||||
|
||||
// (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
|
||||
return iterate(first(x))(first(y));
|
||||
})();
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue