I've been learning currying in Lua and came across the following code:
function addup(x)
local sum = 0
local function f(n)
if type(n) == "number" then
sum = sum + n
return f
else
return sum
end
end
return f(x)
end
print(addup (1) (2) (3) ()) --> 6
print(addup (4) (5) (6) ()) --> 15
I assume that the (1) (2) (3) () part means that the values are passed to the function in succession rather than simultaneously. My problem is that I can't quite figure out how exactly these values are passed and how the function manipulates them. So the first value goes to x, but what happens to the second? Is it passed as n into the inner function? Then where goes the third one?
My other problem is with the line:
return f
Here a function returns itself without any value or even parentheses. I know that you can pass in a string or a table to a function omitting the parentheses but it is not the case here. So what's going on?
I looked in the manuals but didn't find anything on either of these features. I'd really appreciate an explanation or a link to a manual that covers this in detail. Thanks.
Update: link to the tutorial I referred to - http://lua-users.org/wiki/CurriedLua
As it was pointed out in one of the anwsers, there is no (x)(y) syntax in lua. What it is, is a series of function calls. This wasn't actually mentioned in the tutorial but was my assumption which turned out to be wrong, so I edited this bit out.
The issue is solved. Thank you for all your answers, they were very helpful.
add5=addup(5); print(add5 (3) ()); print(add5 (2) (), as it would print8and10instead of8and7.