can auto-generate comparison functions for composed types
This commit is contained in:
parent
0b262daf7f
commit
4d1fb81492
6 changed files with 72 additions and 35 deletions
|
|
@ -1,4 +1,4 @@
|
|||
import { Char, Double, Int } from "../primitives/types.js";
|
||||
import { Char, Double, Int, Unit } from "../primitives/types.js";
|
||||
|
||||
export const compareNumbers = x => y => {
|
||||
if (typeof(x) !== 'number' || typeof(y) !== 'number') {
|
||||
|
|
@ -21,9 +21,5 @@ export const compareBools = x => y => {
|
|||
return x - y;
|
||||
};
|
||||
|
||||
export const typeToCmp = new Map([
|
||||
[Int, compareNumbers],
|
||||
[Char, compareStrings],
|
||||
[Double, compareNumbers],
|
||||
[Boolean, compareBools],
|
||||
]);
|
||||
// The Unit-type has only one instance, which is equal to itself:
|
||||
export const compareUnits = x => y => 0;
|
||||
|
|
|
|||
25
compare/registry.js
Normal file
25
compare/registry.js
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
import { SymbolBool, SymbolChar, SymbolDouble, SymbolInt, SymbolUnit } from "../primitives/types.js";
|
||||
import { symbolList, symbolProduct, symbolSet, symbolSum } from "../structures/types.js";
|
||||
import { compareBools, compareNumbers, compareStrings, compareUnits } from "./primitives.js";
|
||||
import { compareLists, compareProducts, compareSets, compareSums } from "./structures.js";
|
||||
|
||||
const typeSymbolToCmp = new Map([
|
||||
[SymbolInt , compareNumbers],
|
||||
[SymbolChar , compareStrings],
|
||||
[SymbolDouble, compareNumbers],
|
||||
[SymbolBool , compareBools],
|
||||
[SymbolUnit , compareUnits],
|
||||
|
||||
// these functions take extra comparison callbacks:
|
||||
[symbolList , compareLists],
|
||||
[symbolProduct, compareProducts],
|
||||
[symbolSum , compareSums],
|
||||
[symbolSet , compareSets],
|
||||
])
|
||||
|
||||
export const makeCompareFn = type => {
|
||||
return type.params.reduce(
|
||||
(acc, cur) => acc(makeCompareFn(cur)),
|
||||
typeSymbolToCmp.get(type.symbol)
|
||||
);
|
||||
}
|
||||
|
|
@ -1,8 +1,10 @@
|
|||
import { compareNumbers } from "./primitives.js"
|
||||
import { length as lengthLs } from "../structures/list.js";
|
||||
import { get, 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";
|
||||
import { makeGeneric } from "../generics/generics.js";
|
||||
import { lsType } from "../structures/types.js";
|
||||
|
||||
// (a -> a -> Int) -> [a] -> [a] -> Int
|
||||
export const compareLists = compareElems => x => y => {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue