create 'Ordering' type

This commit is contained in:
Joeri Exelmans 2025-05-09 16:35:26 +02:00
parent 77dfc8b182
commit b0023afe8c
15 changed files with 158 additions and 117 deletions

View file

@ -1,18 +1,25 @@
// Total ordering of primitive types
export const compareNumbers = x => y => {
export const compareInts = x => y => {
if (typeof(x) !== 'bigint' || typeof(y) !== 'bigint') {
throw new Error(`was only meant to compare bigints! got ${x} and ${y}`);
}
return (x < y) ? -1 : (x > y) ? 1 : 0;
};
export const compareDoubles = 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') {
@ -26,3 +33,5 @@ export const compareUnits = _ => _ => 0;
// Symbols are encoded as strings
export const compareSymbols = a => b => compareStrings(a)(b);
export const compareOrderings = a => b => compareDoubles(a)(b);