Code Monkey home page Code Monkey logo

pattern-js's Introduction

pattern-js

Fluent pattern matching for CoffeeScript and JavaScript.

Implementation of pattern-js is inspired by Functional C#.

Usage

Start with pattern() and end with matching().

Examples

Constant and Variable Pattern

CoffeeScript

pattern = require 'pattern-js'
todo = pattern()
    .value("Mon", -> go work)
    .value("Tue", -> go relax)
    .value("Thu", -> go icefishing)
    .when(
        (val) -> val is "Fri" or val is "Sat"
        (day) ->
            if day is bingoDay
                go bingo
                go dancing)
    .value("Sun", -> go church)
    .any(-> go work)
    .matching()

todo "Tue" # go relax

JavaScript

var pattern = require('pattern-js');

var work = 1,
    relax = 2,
    iceFishing = 3,
    bingo = 4,
    dancing = 5;
var bingoDay = "Fri";
var go = function(day) { console.log(day); };

var todo = pattern()
    .value("Mon", function() { go(work); })
    .value("Tue", function() { go(relax); })
    .value("Thu", function() { go(iceFishing); })
    .when(function(val) {  return val === "Fri" || val === "Sat"; },
            function(day) {
                if (day === bingoDay) {
                    go(bingo);
                    go(dancing);
                }})
    .value("Sun", function() { go(church); })
    .any(function() { go(work); })
    .matching();

todo("Fri") // usage

Matching Multiple Elements

CoffeeScript

detectZeroTuple = pattern()
    .value([0, 0], -> console.log "Both zero")
    .some(
        [0, undefined]
        (var1, var2) -> console.log "First value is 0 in (0, #{ var2 })")
    .some(
        [undefined, 0]
        (var1, var2) -> console.log "Second value is 0 in (#{ var1 }, 0)")
    .any(-> console.log "Both nonzero.")
    .matching()
detectZeroTuple 0, 0
detectZeroTuple 1, 0
detectZeroTuple 0, 10
detectZeroTuple 10, 15

JavaScript

var detectZeroTuple = pattern()
    .value([0, 0], function() { console.log("Both zero"); })
    .some([0, undefined],
            function(var1, var2) { console.log("First value is 0 in (0, " + var2 + ")"); })
    .some([, 0],
            function(var1, var2) { console.log("Second value is 0 in (" + var1 + ", 0)"); })
    .any(function() { console.log("Both nonzero."); })
    .matching();
detectZeroTuple(0, 0);
detectZeroTuple(1, 0);
detectZeroTuple(0, 10);
detectZeroTuple(10, 15);

pattern-js's People

Contributors

ph200 avatar

Watchers

 avatar

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.