lua-users home
lua-l archive

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

2014-01-31 Rena <>:

> Functions also have metatables, which can be interesting:
>> debug.setmetatable(print, {__concat=function(a,b) return function(...)
>> return b(a(...)) end end})
>> f = string.reverse .. string.upper
>> =f('hello')

This should rather be

__concat=function(a,b) return function(...) return a(b(...)) end end

for two reasons:

1. In mathematics, function composition is right-associative.
2. In Lua, concatenation is right-associative.

It would be confusing if `f=sin..log..cos` and f(x) gave
$ cos log sin x $

In the example, since the two functions commute, there is
no difference, of course.