lua-users home
lua-l archive

Re: lists with nil play nice for Lua 5.2

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


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


AltStyle によって変換されたページ (->オリジナル) /