lua-users home
lua-l archive

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


The definition of table.unpack, as per the manual, is:

--

table.unpack (list [, i [, j]])

Returns the elements from the given list. This function is equivalent to

     return list[i], list[i+1], ···, list[j]

By default, i is 1 and j is #list.

--

I consider this function bad, as it relies on #list. This means it's O(log n) + O(n), but it could be changed to O(n) by changing it to:

--

table.unpack (list [, i [, j]])

Returns the elements from the given list. This function is equivalent to

     return list[i], list[i+1], ···, list[j]

By default, i is 1 and j is the position of the first nil.

--

Same for table.concat: it shouldn't rely on #list but on the first nil, to avoid an unnecessary O(log n) computation.

Because they rely on #list, that also means their results vary across implementations, which is bad.

(Does Lua even need # for anything? It seems that, except for table.sort, everything in Lua would work just as well, if not better, on ipairs())

--
Disclaimer: these emails may be made public at any given time, with or without reason. If you don't agree with this, DO NOT REPLY.