restructure code a bit, add comparison functions for primitive types and composed types (needed to put values in sets)

This commit is contained in:
Joeri Exelmans 2025-04-17 15:11:06 +02:00
parent 3978f7f835
commit 8653bb99c6
12 changed files with 175 additions and 64 deletions

29
compare/primitives.js Normal file
View 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],
]);