lua-users home
lua-l archive

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


sessile@in-gen.net wrote:
> 
> A nifty little script was posted to the Python Tutor list a few days
> ago and for practice I decided to rewrite it using Lua-5.0-alpha.
> The conversion was simple except that I couldn't think of an elegant
> way to emulate the tuple-indexed dictionary.  The initialization of
> STARTING_ROW doesn't feel quite right either though it does work (and
> that, I suppose, is the most important part).
> 
> How would you have written this in Lua?  My working attempt is included
> at the bottom.
/snip
> === begin ascii-art.lua ===
> 
> RULE_100 = { ["111"] = 0,
>               ["110"] = 1,
>               ["101"] = 1,
>               ["100"] = 0,
>               ["011"] = 1,
>               ["010"] = 1,
>               ["001"] = 1,
>               ["000"] = 0 }
> 


Well, why not use this one? ^_^

RULE_100 = { [{1,1,1}]=0; [{1,1,0}]=1; 
             [{1,0,1}]=1; [{1,0,0}]=0;
             [{0,1,1}]=1; [{0,1,0}]=1; 
             [{0,0,1}]=1; [{0,0,0}]=0; 
           }
Tuples or triples can be written as tables in LUA.
And LUA tables are orthogonal, so you can use anything,
including a table as a table index. You'll still 
need to write some functions to compare these table/tuples, 
as the lua = operator compares the adddress of the tables only.
But, by modifying the metatable of the tuples, you could change 
that behaviour.

Hmmm... I think I'll do a small n-tuple library in pure Lua
if I have time, tomorrow.

-- 
"No one knows true heroes, for they speak not of their greatness." -- 
Daniel Remar.
Björn De Meyer 
bjorn.demeyer@pandora.be