lua-users home
lua-l archive

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


On Mon, 10 May 2010, Arseny Vakhrushev wrote:

> >> Could x[] be syntactic sugar for x[#x+1] ?
> >> It would make appending to lists so much nicer.

I'm not all that excited about this syntax.  It just doesn't push any 
list-append or last-element buttons in me.

> > 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.
> 
> The # operator has constant complexity I suppose. It doesn't deal with the associative part of a
> table at all. Moreover, it is not a true "length" operator for the dense part as it works that way
> only for well-managed sequences.

You'd think so, but it is O(log n) usually.  The worst case complexity 
of # was recently discussed on IRC in #lua, and it turns out that # is 
actually O(n) for suitably crafted tables!  Colour me surprised.

Cheers,

Joonas