0.1.0 • Published 5 years ago

algebraic-types v0.1.0

Weekly downloads
4
License
MIT
Repository
github
Last release
5 years ago

Algebraic Types

Algebraic Types is a small library with largely the same surface area as daggy, with a couple of small differences.

  1. Daggy's cata is known as match, and will explicitly fail if exhaustive type matching is not performed.
  2. Constructor variables are themselves tagged using either variable, recursive or typedVariable 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
0.1.0

5 years ago

0.0.1

5 years ago