dope2/lib/structures/sum.js

11 lines
466 B
JavaScript

// Sum-type (also called: tagged union, disjoint union, variant type)
// A Sum-type always has only two variants, called "left" and "right".
// Sum-types of more variants (called Enums) can be constructed by nesting Sum-types.
export const newLeft = left => ({t: "L", v: left });
export const newRight = right => ({t: "R", v: right});
export const match = sum => leftHandler => rightHandler =>
sum.t === "L"
? leftHandler(sum.v)
: rightHandler(sum.v);