npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

pmatch-js

v1.0.7

Published

ES6 pattern matching library for functional programming - inspired by Haskell

Downloads

382

Readme

pmatch-js

Powerful pattern matching for js - inspired by Haskell.

Install

npm install pmatch-js

Testing

Run npm test - requires chai and mocha

Examples

Notes:

  • The otherwise call is necessary and must be the last call that you make.
  • '_' acts as a wildcard. I liked the smiley :-)
  • Its implemented via recursion. Just that you know.
  • uses ES6 syntax.

Calculate fibonacci

const fibbonacci = n => {
        const go = (n, a, b) => match(n)
                .when(0, a)
                .otherwise(_ => go(n - 1, b, a +b));

        return go(n, 0, 1);
}

console.log(fibbonacci(25));
// 75025

Calculate factorial

const factorial = n => match(n)
        .when(0, 1)
        .otherwise(n => n * factorial(n - 1));

console.log(factorial(10))
// 3628800

list length

const tail = ([_, ...xs]) => xs;
const head = ([x, ..._]) => x;

const count = c => match(c)
        .when([], 0)
        .otherwise(xs => 1 + count(tail(xs)));

console.log(count([1,2,3,4,5]));
// 5

Map f over a list

const map = (f, xs) => match(xs)
        .when([], [])
        .otherwise(([x, ...xs]) => [f(x), ...map(f, xs)]);

console.log(map(Number, ['1', '2', '3', '4', '5']));
[ 1, 2, 3, 4, 5 ]

Recursive zipWith

const zipWith = (f, lst1, lst2) => match([lst1, lst2])                                                           
        .when([[], '_'], [])                                                                                     
        .when(['_', []], [])                                                                                     
        .otherwise(([[x, ...xs], [y, ...ys]]) => [f(x, y)].concat(zipWith(f, xs, ys)));                          
                        
console.log(zipWith((a, b) => a + b, [4,2,5,6], [2,6,2,3]));                                                     
// [ 6, 8, 7, 9 ]
                        
console.log(zipWith((a, b) => `${a} ${b}`, ['foo', 'bar', 'baz'], ['fighters', 'hoppers', 'aldrin']));           
// [ 'foo fighters', 'bar hoppers', 'baz aldrin' ]
                
console.log(zipWith(Math.max, [6,3,2,1], [7,3,1,5]));
// [ 7, 3, 2, 5 ]]

Walk-a-tree / map f over Tree

class Tree {
        constructor(left, right) {
                this.left = left;
                this.right = right;
        }
}

class Node {
        constructor(value) {
                this.value = value;
        }
}

const T = (l, r) => new Tree(l, r);
const N = v => new Node(v);

const walkT = t => match(t)
        .when(Node, v => console.log(v.value))
        .when(Tree, t => { walkT(t.left); walkT(t.right)})
        .otherwise(_ => 'error');
        
const mapT = (f, t) => match(t)
        .when(Node, v => N(f(v.value)))
        .when(Tree, t => T(mapT(f, t.left), mapT(f, t.right)))
        .otherwise(_ => { throw new Error('error') });

const pow = v => Math.pow(v, 2);

const mapped = mapT(pow, T(T(N(5), T(N(7), N(1))), T(N(-1), N(8))));

walkT(mapped);
// 25
// 49
// 1
// 1
// 64

Some deeper patterns: Note: 'string' and 'number' are the typeof's. So we can match on those as well...

const m1 = match('Hello')
	.when('string', a => [true, a.toUpperCase()])
	.when('number', a => [false, a])
	.otherwise(_ => [false, 'no match'])

L(m1);

const m2 = match(['Hello', 5])
	.when(['string', 'string'], ([a, b]) => [false, a, b])
	.when(['string', 'number'], ([a, b]) => [true, a, b])
	.otherwise(_ => [false, 'no match'])

L(m2);

some more difficult ones:

const m5 = match(['Hello', [100, 100], 100])
	.when(['Hello', [5, 5], '_'], ([a, b, c]) => [false, a, b, c])
	.when(['Hello', [100, 100], p => p < 100], ([a, b, c]) => [false, a, b, c])
	.when(['Goodbye', ['_', '_'], '_'], ([a, b, c]) => [false, a, b, c])
/* -> */.when(['_', [100, 100], p => p === 100], ([a, b, c]) => [true, a, b, c])
	.otherwise(_ => [false, 'no match'])

L(m5);

const anyObject = {
	a: 5,
	b : {
		c: 4
	}
};

const m7 = match(anyObject)
	.when({a : 5}, false)
	.when({a : 5, b : { c : 3 }}, false)
	.when({a : 5, b : { c : '_' }}, true)
	.otherwise(false);

Can also match own types in nested arrays:

class ABox {
	constructor(v) {
		this.v = v;
	}
}

const m6 = match([new ABox(5), [new ABox(10), new ABox(20)]])
	.when([ABox, 5], ([a, b]) => [false, a, b])
	.when([ABox, [ABox, ABox]], ([a, b]) => [true, a, b])         // <- will match
	.otherwise(_ => 'no match');

L(m6);