[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: declarative programming, table constructors, multiple returns
- From: "Carl Dougan" <carl.dougan@...>
- Date: Tue, 6 Nov 2007 00:03:22 -0800
> What's the point of doing {{"r",1},{"g",2},{"b",3},'etc'}?
>
> This requires indexing first by index for the table and then by the
> color letter which seems redundant to me. Would it be better like
> {r=1,g=2,b=3,'etc'}?
>
> wes
That was just a contrived example.
Here's another (equally contrived) example where multi rv's may
actually be useful
function fields(tbl,...)
local t
local stringColor = {r=1,g=2,b=3}
local keys={...}
for _,k in ipairs(keys) do
local a = { label=tostring(k), value=tostring(tbl[k]) }
if type(a)=="string" then a.color = stringColor end
t[#t+1] = a
end
return unpack(t)
end
x = {a=1, b=2, c=3}
y = {n='fred'}
def = {fields(x,'a','b','c'), fields(y,'n')}
def2 = {{label="a", value=1},{label="b", value=2},{label="c",
value=3},{label="n", value='fred', color={r=1,g=2,b=3}}}
def seems a nice way to get def2. Hopefully that shows the motivation
a little better.
Of course in this particular example you could make def2 any number of
ways. Its just to show the idea. For complex table declarations (which
need to be as simple and clear as possible) using multi rv functions
like that can help a lot, at least imho.
Carl
>
> On 11/5/07, Carl Dougan <carl.dougan@gmail.com> wrote:
> > On 11/5/07, Carl Dougan <carl.dougan@gmail.com> wrote:
> > > > There's a patch which sounds similar to what you want:
> > > > http://lua-users.org/wiki/LuaPowerPatches and grep table, should be
> > > > the first hit.
> > > >
> > > > Ben
> > > >
> > >
> > > Thanks a lot. That solves the problem.
> > > It would be nice not to have to use a patched lua though.
> > > I hope the patch or something similar makes it into the language.
> > > Carl
> > >
> >
> > hmm.. I'm using LuaJIT - so it probably wont be trivial to apply the
> > patch. oh well
> >
>
- References:
- declarative programming, table constructors, multiple returns, Carl Dougan
- Re: declarative programming, table constructors, multiple returns, Ben
- Re: declarative programming, table constructors, multiple returns, Carl Dougan
- Re: declarative programming, table constructors, multiple returns, Carl Dougan
- Re: declarative programming, table constructors, multiple returns, Wesley Smith