lua-users home
lua-l archive

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


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.