52 lines
2 KiB
JavaScript
52 lines
2 KiB
JavaScript
import { makeCompareFn } from "../lib/compare/dynamic.js";
|
|
import { Int, Unit } from "../lib/primitives/primitive_types.js";
|
|
import { unit } from "../lib/primitives/unit.js";
|
|
import { makeConstructors, makeMatchFn } from "../lib/structures/enum.js";
|
|
import { enumType } from "../lib/structures/enum.types.js";
|
|
import { newProduct } from "../lib/structures/product.js";
|
|
import { lsType } from "../lib/structures/type_constructors.types.js";
|
|
import { prettyT } from "../lib/util/pretty.js";
|
|
|
|
Error.stackTraceLimit = Infinity;
|
|
|
|
const variants = [
|
|
newProduct("price")(_ => Int),
|
|
newProduct("prices")(_ => lsType(_ => Int)),
|
|
newProduct("not_found")(_ => Unit),
|
|
];
|
|
|
|
const structuralType = enumType(variants);
|
|
|
|
console.log("observe the type that was generated:");
|
|
console.log(" ", prettyT(structuralType));
|
|
|
|
const [newPrice, newPrices, newNotFound] = makeConstructors(variants);
|
|
|
|
const price = newPrice(10);
|
|
const prices = newPrices([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}`)
|
|
(() => "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(structuralType);
|
|
|
|
console.log("observe the generated compare function in action:");
|
|
console.log(" should be smaller ->", compareMyEnum(price)(prices));
|
|
console.log(" should be bigger ->", compareMyEnum(prices)(price));
|
|
console.log(" should be bigger ->", compareMyEnum(notFound)(price));
|
|
console.log(" should be equal ->", compareMyEnum(prices)(prices));
|
|
console.log(" should be smaller ->", compareMyEnum(newPrice(5))(newPrice(6)));
|
|
console.log(" should be bigger ->", compareMyEnum(newPrices([5, 6]))(newPrices([5, 5])));
|