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
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)
|
||||
);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue