[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Most awesome string metatable hack
- From: Dirk Laurie <dirk.laurie@...>
- Date: Sat, 1 Feb 2014 08:08:24 +0200
2014-01-31 Rena <hyperhacker@gmail.com>:
> 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')
> OLLEH
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.