move test to separate file

This commit is contained in:
Joeri Exelmans 2025-04-17 16:48:17 +02:00
parent 4d1fb81492
commit bc9dce4b9c
2 changed files with 50 additions and 47 deletions

48
scripts/enum.js Normal file
View file

@ -0,0 +1,48 @@
import { makeCompareFn } from "../compare/registry.js";
import { Int, Unit } from "../primitives/types.js";
import { unit } from "../primitives/unit.js";
import { enumType, makeConstructors, makeMatchFn } from "../structures/enum.js";
import { constructorProduct } from "../structures/product.js";
import { lsType, prettyT } from "../structures/types.js";
const variants = [
constructorProduct("price")(Int),
constructorProduct("prices")(lsType(Int)),
constructorProduct("not_found")(Unit),
];
const myEnumType = enumType(variants);
console.log("observe the type that was generated:");
console.log(" ", prettyT(myEnumType));
const [newPrice, newPrices, newNotFound] = makeConstructors(variants);
const price = newPrice(10);
const prices = newPrices({ l: [20, 30] });
const notFound = newNotFound(unit);
console.log("observe the encoding of different variant instances:");
console.log(" ", price);
console.log(" ", prices);
console.log(" ", notFound);
const myEnumToString = x => makeMatchFn(variants)(x)
(price => `Price: ${price}`)
(prices => `Prices: ${prices.l}`)
(() => "Not found!");
console.log("observe the generated match function in action:");
console.log(" ", myEnumToString(price));
console.log(" ", myEnumToString(prices));
console.log(" ", myEnumToString(notFound));
const compareMyEnum = makeCompareFn(myEnumType);
console.log("observe the generated compare function in action:");
console.log(" smaller ->", compareMyEnum(price)(prices));
console.log(" bigger ->", compareMyEnum(prices)(price));
console.log(" bigger ->", compareMyEnum(notFound)(price));
console.log(" equal ->", compareMyEnum(prices)(prices));
console.log(" smaller ->", compareMyEnum(newPrice(5))(newPrice(6)));
console.log(" bigger ->", compareMyEnum(newPrices({ l: [5, 6] }))(newPrices({ l: [5, 5] })));

View file

@ -1,10 +1,8 @@
import { makeCompareFn } from "../compare/registry.js";
import { Bottom, Int, Unit } from "../primitives/types.js";
import { unit } from "../primitives/unit.js";
import { Bottom } from "../primitives/types.js";
import { capitalizeFirstLetter } from "../util/util.js";
import { constructorProduct, getLeft, getRight } from "./product.js";
import { constructorLeft, constructorRight, match } from "./sum.js";
import { lsType, prettyT, sumType } from "./types.js";
import { sumType } from "./types.js";
// 'variants' is an array of (name: string, type: Type) pairs.
// e.g., the list of variants:
@ -57,46 +55,3 @@ export const makeConstructors = variants => {
({[ctor.name]: val => constructorRight(ctor(val))}[ctor.name])),
];
}
///////////////////////////////////////////////////////////////////////////////
const variants = [
constructorProduct("price")(Int),
constructorProduct("prices")(lsType(Int)),
constructorProduct("not_found")(Unit),
];
const myEnumType = enumType(variants);
console.log("observe the type that was generated:");
console.log(prettyT(myEnumType));
const [newPrice, newPrices, newNotFound] = makeConstructors(variants);
const price = newPrice(10);
const prices = newPrices({l:[20,30]});
const notFound = newNotFound(unit);
console.log("observe the encoding of different variant instances:");
console.log(price);
console.log(prices);
console.log(notFound);
const myEnumToString = x => makeMatchFn(variants)(x)
(price => `Price: ${price}`)
(prices => `Prices: ${prices.l}`)
(() => "Not found!");
console.log("observe the generated match function in action:")
console.log(myEnumToString(price));
console.log(myEnumToString(prices));
console.log(myEnumToString(notFound));
const compareMyEnum = makeCompareFn(myEnumType);
console.log(compareMyEnum(price)(prices)); // smaller
console.log(compareMyEnum(prices)(price)); // bigger
console.log(compareMyEnum(notFound)(price)); // bigger
console.log(compareMyEnum(prices)(prices)); // equal
console.log(compareMyEnum(newPrice(5))(newPrice(6))); // smaller