Giter VIP home page Giter VIP logo

headpats's Introduction

Headpats

Pattern matching and tagged unions in JavaScript without new syntax.
See the guide on how to use Headpats!
Refer to the documentation for specific information on pattern matching and built-in features.

Examples

Setup

const pat = require('headpats');
const { $, $$, _, is, rest } = pat;

Stringly Typed

function doOperation(operation, a, b) {
    return pat
        .case('add', () => a + b)
        .case('minus', () => a - b)
        .case(_, () => NaN)(operation);
}

doOperation('add', 1, 2)
 3

doOperation('minus', 1, 2)
 -1

doOperation('something', 1, 2)
 NaN

Safe Traversal

const o = { x: { y: { z: 10 } } };

pat.match({ x: { y: { z: $.z } } }, o)
 { z: 10 }

pat.match({ x: { y: { what: $.what } } }, o)
 null

Result Matching

const ok = Symbol('ok');
const err = Symbol('err');

function divide(a, b) {
    if (b === 0) {
        return [err, null];
    }

    return [ok, a / b];
}

pat.match([ok, $.num], divide(10, 2));
 { num: 5 }

Option Type

const Option = pat.union('Option', 'Some', 'None');
const { Some, None } = Option;

const double = pat
    .case($$(Some, $.x), ({ x }) => new Some(x * 2))
    .case($$(None, _), () => new None());

double(new Some(5))
 Some { x: 10 }

double(new None())
 None {}

double(100)
 Error

Recursive Map

const map = pat
    .clause([], _, () => [])
    .clause([$.x, [rest, $.xs]], $.f, ({ x, xs, f }) => [f(x)].concat(map(xs, f)));

map([1, 2, 3, 4], x => x * 2)
 [2, 4, 6, 8]

headpats's People

Contributors

1computer1 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

teeseal

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.