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 { newProduct } from "../structures/product.js"; import { lsType, prettyT } from "../structures/types.js"; const variants = [ newProduct("price")(Int), newProduct("prices")(lsType(Int)), newProduct("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] })));