5 ms·
Show HN: Matches.js -- Powerful Pattern Matching for Javascript
- ntoshev 14y agoI've played a bit with pattern matching as a DSL on top of JavaScript: http://jsfiddle.net/ntoshev/q8Ayw/1/ http://jsfiddle.net/ntoshev/q8Ayw/1/
- username3 14y agoWhat are some applications of pattern matching?
- natefaubion 14y agoPattern matching is common in functional languages like Haskell or Scala. It's like a form of overloading on steroids, letting you inspect the values and structure of the arguments passed to your function (with a very terse syntax), and do different things based on that criteria. It can also take care of a lot of boilerplate like splitting an array into different parts or extracting values from a configuration object.
- tlack 14y agoYou know how a lot of jQuery functions take either a string, or a function, or an array of strings, etc? Instead of making those decisions in your code in a series of IF statements, you could use this library and just maintain a "map" of the different argument forms and have this library dispatch the right option. In Erlang, this is used all the time to make functions that behave differently depending on how they're called - for instance, massaging a binary string into a regular string if the function only expects a regular string. This is a small tool in a programmer's toolkit, but could be a handy one.
- agumonkey 14y agoThink of it as declarative nested if statements. Concise like a switch but generalized to typed values.
- elliotlai 14y agoi want this in js for a long time
- se85 14y agoI've come across aspects of this functionality before in a couple of languages now, even psql, however looking at this JavaScript way of doing it, I have to question whether it helps code readability or hurts it. edit: dyslexia
- natefaubion 14y agoTake a look at the next sentence: "Keep in mind, that time is measured in the single microseconds (1µs vs 3µs) to dispatch 5 calls to the same function." So you are still looking at sub-microsecond dispatch time for a single call. If you look at benchmark/compare.js you'll see how much shorter and clearer the pattern matched function is. I doubt this will put a strain on your performance. edit: That said, there is likely room for optimizations and speed improvement. It will naturally take longer because it has the overhead of making function calls to the various pattern functions to check if there's a match, instead of having it all inlined like in the hand optimized function. edit2: Sorry, this was in response to another comment that looks like it got deleted while I was editing. Now its attached itself elsewhere.
- se85 14y agoThanks, I misread the first time, made sense the second time! My mistake! I still have my doubts as to how readable such an approach like this would be in large JavaScript application's when taking into account the already very dynamic nature of JavaScript, but thats more of a personal opinion than anything else.
- OmarIsmail 14y agoI 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.
- deleted 14y ago[deleted]
- mtrimpe 14y agoFor me it was realizing that // TypeError: "All patterns exhausted" means it's also perfect for limited, but good enough, checking of input parameters.
- jpolitz 14y agoCool! 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 http://stackoverflow.com/a/280861)
- natefaubion 14y agoYou 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.
- pufuwozu 14y agoSucks 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 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.
- danh 14y agoSomething like this would make a great addition to CoffeeScript.
- natefaubion 14y agoIt 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.
- oscilloscope 14y agoCheck out LiveScript's implicit switch syntax: http://gkz.github.com/LiveScript/#switch http://gkz.github.com/LiveScript/#switch
- natefaubion 14y agoThat's along similar lines, but more like guards in Haskell: http://en.wikibooks.org/wiki/Haskell/Control_structures http://en.wikibooks.org/wiki/Haskell/Control_structures
- ble 14y agoLast 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#issuecomment-1645482 https://github.com/jashkenas/coffee-script/issues/1419#issue... [2]: http://www.reddit.com/r/programming/comments/er0qj/coffeescript_hits_10_happy_holidays_proggit/c1a907o http://www.reddit.com/r/programming/comments/er0qj/coffeescr...
- pooriaazimi 14y agoFor those interested: XRegExp (http://xregexp.com http://xregexp.com) is also relevant. (Not a direct competitor to Matches.js, but has some nice features)
- dreamdu5t 14y agoAwesome! This is so useful!