[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: Sat, 4 Aug 2007 15:29:06 +0200
On Aug 04, 2007, at 11:23, Duck wrote:
So don't have holes in arrays :-)
Well... varargs routinely have nil values in them.... don't use varargs
then?!? At that rate, not much will be left of Lua. Plus, there is no
compelling reason for such a small, tight language to be littered with
body-traps, isn't it?
In any case, here is a revisited 'tuple' implementation of sort:
Tuple = function( ... )
local aList = { n = select( '#', ... ), ... }
return function( aKey, anIndex )
local anIndex = aKey or ( ( anIndex or 0 ) + 1 )
local aValue = aList[ anIndex ]
if aKey == '#' then
return aList.n
elseif aKey == '*' then
return unpack( aList, 1, aList.n )
elseif aKey then
return aValue
elseif anIndex <= aList.n then
return anIndex, aValue
end
end
end
Usage example:
local aTuple = Tuple( nil, 'a', nil, 'b', nil, 'c', nil )
print( aTuple( '#' ) ) -- count
print( aTuple( '*' ) ) -- all values
print( aTuple( 2 ) ) -- one value
for anIndex, aValue in aTuple do -- enumerate
print( anIndex, aValue )
end
> 7
> nil a nil b nil c nil
> a
> 1 nil
> 2 a
> 3 nil
> 4 b
> 5 nil
> 6 c
> 7 nil