[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: cons cells for Lua
- From: Mark Hamburg <mhamburg@...>
- Date: Thu, 25 Mar 2004 09:02:25 -0800
on 3/25/04 8:45 AM, Jay Carlson at email@example.com wrote:
> The reason to use  and  is they take advantage of the "small integer
> keys are stored in an array" optimization in Lua 5.0. For clarity you could
> use .car and .cdr instead. Note that all strings in Lua are interned, so
> the performance penalty is not that great.
Do you want to use 0 and 1 or 1 and 2?
One other thing to try is doing these as function closures.
function cons( a, b )
return function( key )
if key == "car" then
elseif key == "cdr" then
function car( a )
return a( "car" )
function cdr( a )
return a( "cdr" )
> On the other hand, it might be a fun exercise to implement this as a C
> extension. The car would be stored as a ref, which is something the
> reference manual doesn't emphasize enough. (Somebody might wanna write a
> tutorial for the wiki on references). If you allow improper lists, you'll
> have to store the cdr as a ref as well.
Beware of what amount to reference-counting cycles when creating refs. It's
actually a bit of a pain to get userdata to refer safely to other Lua data
objects and seems to require the use of per-object metatables rather than
sharing the metatables across a type.