Live data from Hacker News

Show HN: Matches.js -- Powerful Pattern Matching for Javascript

github.com

21–23 of 23 posts

Re: Show HN: Matches.js -- Powerful Pattern Matching for Javascript

#21
post #10

Cool! Sucks that it has to be so stringly typed, but I can't see another alternative. One concern: "Each pattern will be tried in order until a match is found." As far as I know, ECMAScript doesn't specify any enumerations on objects that obey an order... browsers do syntactic order, but it's by no means a guarantee. (e.g. http://stackoverflow.com/a/280861 )

Sucks that it has to be so stringly typed, but I can't see another alternative.

Multimethods would be an alternative to this type of ad-hoc polymorphism. I've encoded immutable multimethod environments in my new library, bilby.js:

https://github.com/pufuwozu/bilby.js

Lets you write things like this:

    var env = λ.environment()
        .method('length', λ.isArray, function(a) {
            return a.length;
        })
        .method('length', λ.isString, function(s) {
            return s.length;
        })
        .property('empty', function(o) {
            return !this.length(o);
        });

    env.empty([]) == true;
    env.empty([1, 2, 3]) == false;
Where isArray and isString are any functions that return true/false based on the input arguments. The environment then dispatches whichever method that has a predicate return true first.

Re: Show HN: Matches.js -- Powerful Pattern Matching for Javascript

#23
post #14

Something like this would make a great addition to CoffeeScript.

Last I heard, jashkenas was not a fan of adding such.

"... [P]attern matching inspired by static languages is a particularly poor fit for JavaScript (and by extension CoffeeScript), because types are extremely weak in JS." -jashkenas [1]

"[... P]attern matching is not nearly as useful in a language without rich types [...]" - jashkenas [2]

I'm on that reddit thread, respectfully disagreeing with him.

[1]: https://github.com/jashkenas/coffee-script/issues/1419#issue...

[2]: http://www.reddit.com/r/programming/comments/er0qj/coffeescr...

Post reply on HN