lua-users home
lua-l archive

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


On 10 May 2010 10:05, Marc OMorain <marc.omorain@kore.net> wrote:
> Could x[] be syntactic sugar for x[#x+1] ?
> It would make appending to lists so much nicer.

I would worry about the hidden performance implications of this. The
magic # operator performs a binary search of both storage parts of the
table, which is an O(log N) operation.

It would be nicer to have a general table insert function, that puts a
value into the table with some unspecified key that does not already
exist in the table, allowing the user to easily use a table like a
set.

Assuming object equality <=> object identity, you could use the value as a key, and just stick true in the value. This makes some set operations more efficient, and simplifies the addition of new elements.

If you do want to have a quick way of finding the next index, you could always store it in a field, such as 'n'.

    henk