algebraic-types
v0.1.0
Published
Algebraic Types is a small library with largely the same surface area as [daggy](https://github.com/fantasyland/daggy), with a couple of small differences.
Downloads
4
Readme
Algebraic Types
Algebraic Types is a small library with largely the same surface area as daggy, with a couple of small differences.
- Daggy's
cata
is known asmatch
, and will explicitly fail if exhaustive type matching is not performed. - Constructor variables are themselves tagged using either
variable
,recursive
ortypedVariable
functions. This allows constructors to fail on invalid inputs.
Example
const {
createUnionType,
recursive,
variable
} = require('algebraic-types');
const BinaryTree = createUnionType('BinaryTree', {
Leaf: [variable('a')],
Branch: [recursive('left'), recursive('right')]
});
const myTree = BinaryTree.Branch(
BinaryTree.Leaf(42),
BinaryTree.Leaf(43)
);
myTree.match({
Leaf: x => x * x
});
// Error: Non exhaustive pattern matching. Branch missing