Sparkler -- Native pattern matching for JavaScript using sweet.js macros
github.com2 pointsby natefaubion0 comments
MyModule = do ->
# module body
No need for any parens. 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. stylus -u nib <file>