[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: lists with nil play nice for Lua 5.2
- From: PA <petite.abeille@...>
- Date: Wed, 1 Aug 2007 17:47:01 +0200
On Aug 01, 2007, at 13:30, Roberto Ierusalimschy wrote:
Another option is to implement tuples. We can implement them as C
functions with upvalues (PiL2, p. 257). With a few extra lines of code,
we get the following:
a = new.tuple(2, nil, "foo")
#a -- not available; perhaps a"#" ???
a:insert("baz") -- not available (tuples are immutable)
a(3) -- yields "foo"
a() -- yields 2, nil, "foo"
for i, v in a do ... -- or something like this...
Here is a straight Lua implementation of sort:
local Tuple = require( 'Tuple' )
local aTuple = Tuple( 2, nil, 'foo', nil )
print( #aTuple )
print( aTuple( 3 ) )
for anIndex, aValue in aTuple do
print( anIndex, aValue )
end
> 4
> foo
> 1 2
> 2 nil
> 3 foo
> 4 nil
Attachment:
Tuple.lua
Description: Binary data