Show HN: Matches.js -- Powerful Pattern Matching for Javascript
1–10 of 23 posts
Re: Show HN: Matches.js -- Powerful Pattern Matching for Javascript
#2Re: Show HN: Matches.js -- Powerful Pattern Matching for Javascript
#3What are some applications of pattern matching?
Re: Show HN: Matches.js -- Powerful Pattern Matching for Javascript
#4What are some applications of pattern matching?
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.
Re: Show HN: Matches.js -- Powerful Pattern Matching for Javascript
#5Re: Show HN: Matches.js -- Powerful Pattern Matching for Javascript
#6edit: dyslexia
Re: Show HN: Matches.js -- Powerful Pattern Matching for Javascript
#7Re: Show HN: Matches.js -- Powerful Pattern Matching for Javascript
#8I'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
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.
Re: Show HN: Matches.js -- Powerful Pattern Matching for Javascript
#9I'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
Take 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 li…
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.
Re: Show HN: Matches.js -- Powerful Pattern Matching for Javascript
#10One 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)