[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Recipe: setfenv with Lua 5.2.0 work3
- From: steve donovan <steve.j.donovan@...>
- Date: Tue, 15 Jun 2010 15:30:57 +0200
On Tue, Jun 15, 2010 at 9:06 AM, David Manura <dm.lua@math2.org> wrote:
> table {
> function()
> local x = 'test'
> tr {
> function()
> for i=1,3 do td { index } end
> end
> }
> end
> }
A little macro magic is useful in such cases:
table {
tr (
_for(i = 1,3, td{i})
)
}
where _for(L,X) expands to (function() local res = {}; for L do
res[#res+1]=(X) end; return res; end)()
Another statement-as-expression macro useful in this way would be
_if(C,X,Y) (function() if C then return X else return Y end)() which
has the useful property that Y is only evaluated when the condition is
false.
(BTW this is reminiscent of how HTML tags can be used in Orbit; it's a
nice example of setfenv in action, where any unknown symbol is treated
as a function creating a tag. )
steve d.