Posted 12 August 2016 - 07:36 PM
I found an article about "Recursion without names and loops" (can't find it anymore) and I tried to port this (IMO really awesome) function over to lua.
The function will calculate the factorial of a given number.
This is the result:
Maybe this is new to someone so I decided to share this with our awesome comunity. I hope that some Lua wizard will find this and invent something interesting with it xD
The function will calculate the factorial of a given number.
This is the result:
(function(f) return f(f) end)(function(func) return function(n) return n == 0 and 1 or (n*func(func)(n-1)) end end)
Spoiler
You use it like this:
local factorialOf10 = (function(f) return f(f) end)(function(func) return function(n) return n == 0 and 1 or (n*func(func)(n-1)) end end)(10)
Maybe this is new to someone so I decided to share this with our awesome comunity. I hope that some Lua wizard will find this and invent something interesting with it xD