My friend today asked me an interesting question about how to write immediately called named functions in CoffeeScript without raising a function variable to the outside.
In JavaScript:
(function factorial(n) { return n <= 1 ? 1 : n * factorial(n-1); })(5);
The best I could think of in CoffeeScript:
do -> do factorial = (n = 5) ->
if n <= 1 then 1 else n * factorial(n-1)
It looks a bit uncomfortable. Is there a better way to do this?
source
share