natefaubion
Joined 15 years ago
Joined 15 years ago
MyModule = do ->
# module body
No need for any parens.[2] https://github.com/natefaubion/adt.js
myfoo = Foo(4, Bar(3, 8))
with patterns:
Foo(x, Bar(3, z)) << myfoo
So in this case, the assertion will only pass if the Foo contains a Bar in the second slot, and that Bar contains a 3 in its first slot, while also binding the 4 and 8 to their own names, x and z respectively. myfn = (arg) ->
return 1 if arg is "one"
return 2 if arg is "two"
return arg.toInt()
This is obviously a little contrived with such a short method, but helps when you have stuff to do between conditional returns. 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.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.
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.