lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Hi Goetz,

On Fri, 19 Mar 2004 around 15:59, you wrote:
> When 2 bracket pairs ()() have to be used?
> I don't understand some cases in which after the 1st pair (containing
> arguments) an empty second pair has to be given.


Functions can return functions. Some very contrived examples:

> mkAdder = function (x)
>> 	return function (y) return x + y end
>> end
> add5 = mkAdder(5)
> =add5(10)
15
> =mkAdder(5)(10)
15
> mkCounter = function (x)
>> 	local i = x - 1
>> 	return function () i = i + 1; return i end
>> end
> =mkCounter(42)
function: 46738
> counter = mkCounter(42)
> =counter()
42
> =counter()
43
> =mkCounter(16)()
16

Not very useful examples I know, I'm sorry.
I guess writing code that ends up stating f(x)() is a bit odd.

Robby