[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: maxn considered pointless. Was: Re: Lua 5.1 (final,rc) now available
- From: Paul Chiusano <paul.chiusano@...>
- Date: Wed, 18 Jan 2006 13:31:29 -0500
> On the other hand, maxn works with sparse arrays only if they're not
> "sparse at the end", as it were. I personally think this is simply a
> bug waiting to get a user of maxn, and that people who actually use
> sparse arrays should come up with their own protocol to record the
> "size" of the array. I've gone back to using a 'n' field, myself...
Another approach is to use mask and unmask nil functions for accessing
the array. Then it's possible to just use the # operator without
modification:
untested:
Nil = {} -- notice captial 'Nil', not nil
function maskNil(v)
return v~=nil and v or Nil
end
function unmaskNil(v)
return v ~= Nil and v or nil
end
function add(t, v)
t[#t+1] = maskNil(v)
end
function get(t, ind)
return unmaskNil(t[ind])
end
etc...
-Paul