Skip to content

bakerface/patmat

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

patmat

Pattern matching in vanilla JavaScript

This package was inspired by Bram Stein's write up. Below are a few examples from that article, which can be used to demonstrate functionality.

Factorial

const { $, match } = require("@bakerface/patmat");

const factorial = match([
  [0, () => 1],
  [$, n => n * factorial(n - 1)]
]);

console.log(factorial(5)); // 120

Binary Tree

const { $, _, type, match } = require("@bakerface/patmat");

const nil = type();
const node = type(t => [Number, t, t]);

const leaves = match([
  [nil, () => 0],
  [node(_, nil, nil), () => 1],
  [node(_, $, $), (a, b) => leaves(a) + leaves(b)]
]);

const toArray = match([
  [nil, () => []],
  [node($, $, $), (x, l, r) => toArray(l).concat(x, toArray(r))]
]);

const tree =
  node(4,
    node(2,
      node(1, nil, nil),
      node(3, nil, nil)),
    node(8,
      node(6,
        node(5, nil, nil),
        node(7, nil, nil)),
      node(9, nil, nil)));

console.log(leaves(tree)); // 5
console.log(toArray(tree)); // [1, 2, 3, 4, 5, 6, 7, 8, 9]

About

Pattern matching in vanilla JavaScript

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published