lua-users home
lua-l archive

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

On Fri, Apr 01, 2011 at 02:25:06PM +0200, H. Diedrich wrote:
> Maybe we could collectively have a friendly, instructive standard answer 
> ready?

Here's my answer:

The length of a table which has holes in it has different possible
meanings.  If Lua's default length operation is not satisfactory for
your application, then what you really need is a data structure which
defines length in the way your application expects.

Here is one possible way to implement a sparse array by Renato Maia:

Other ways are possible, with different conceptions of with length
means in a sparse list.

To re-iterate Renato's point: The Lua table is very flexible, and it
can be used to implement just about any conceivable data structre.

But that doesn't mean it already _is_ any conceivable data structure
with semantics that make sense in all use cases.

People don't complain just because a table can't be used as a stack
without some extra code to push and pop values.  Is this situation with
sparse arrays much different?


One thing that might help here is to have a small standard library
that is strongly endorsed by the entire community.  This library should
have the most commonly used data structures: stacks, sparse arrays,
OOP, etc..  And it should be the go-to library for ordinary usage,
unless you have specific, special requirements.

I've used both Penlight and stdlib, and at least looked at others
like Luis Carvalho's and Matthew Burke's in the Lua Programming Gems
book.  They've all got their good points.  But it would be better to
have just one main one, and have that downloadable from too.

Then, when this comes up (and it will) any of us can just point to the
Lua Standard Library and say: "Use this instead."

James Graves