Search code examples
functionrecursionluamoonscript

How to write recursive function in MoonScript?


Is there something like arguments.callee of JavaScript for MoonScript?


Solution

  • Since Moonscript functions are defined as local func; func = function() end, they are all recursive. This will print 120:

    recursive = (n) -> return n > 1 and n*recursive(n-1) or 1
    print recursive 5
    

    As far as I know, there is no arguments.calee alternative, but I haven't seen cases where I'd need it either. Even Mozilla's docs say "there are nearly no cases where the same result cannot be achieved with named function expressions" about arguments.callee.