Live data from Hacker News

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

github.com

11–20 of 23 posts

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

#11
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 )

You are correct. There is not a guarantee for the future, though I doubt this behavior will change for V8 (this is currently node only). If that scares you, you can use the combinator syntax (check out the readme towards the bottom). The object literal syntax is just sugar for using combinators.

edit: BTW, if you want some precedence in a major library relying on this behavior, look no further than Backbone's router, specifically the `_bindRoutes` method.

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

#15
post #14

Something like this would make a great addition to CoffeeScript.

It meshes pretty well as is. If only I could get rid of the strings and redundant arguments.

  mymap = pattern {
    '_, []' : -> []
    'f, [x, xs...]' : (f, x, xs) -> 
      [f x].concat mymap(f, xs)
  }
It shares a lot of the same splat/rest/destructuring syntax.

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

#16
post #14

Something like this would make a great addition to CoffeeScript.

It meshes pretty well as is. If only I could get rid of the strings and redundant arguments. mymap = pattern { '_, []' : -> [] 'f, [x, xs...]' : (f, x, xs) -> [f x].concat mymap(f, xs) } It shares a lot of the same splat/rest/destructuring syntax.

Check out LiveScript's implicit switch syntax:

http://gkz.github.com/LiveScript/#switch

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

#17

Earlier quoted context omitted.

It meshes pretty well as is. If only I could get rid of the strings and redundant arguments. mymap = pattern { '_, []' : -> [] 'f, [x, xs...]' : (f, x, xs) -> [f x].concat mymap(f, xs) } It shares a lot of the same splat/rest/destructuring syntax.

Check out LiveScript's implicit switch syntax: http://gkz.github.com/LiveScript/#switch

That's along similar lines, but more like guards in Haskell: http://en.wikibooks.org/wiki/Haskell/Control_structures

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

#19

I didn't know I needed this until I saw it and had that A-HA moment when I realized how much cleaner this would make a lot of code. Instead of hiding away the logic in a bunch of conditionals this makes the overloading declarative which is great for maintenance.

For me it was realizing that

    // TypeError: "All patterns exhausted"
means it's also perfect for limited, but good enough, checking of input parameters.
Post reply on HN