lua-users home
lua-l archive

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


On Sun, Sep 11, 2022 at 4:12 AM Paul Ducklin wrote:
Isn’t that code rather arbitrarily limited by the maximum number of arguments a function can handle? So it will only work up to a point, and then error() out unexpectedly?

On 10 Sep 2022, at 22:09, Egor Skriptunoff <egor.skriptunoff@gmail.com> wrote:
On Sat, Sep 10, 2022 at 8:16 PM Alexander Chernoskutov wrote:
I would like to propose a third argument to `table.remove (list [, pos
[, count]])`

`count` - number of elements to remove starting from position `pos`
(defaults to 1). All the removed values are returned.

Right know there's no good way to remove `count` elements from a table.


By using table.move(), it is possible to create implementation with O(#t) time complexity:

function table.remove_count (list, pos, count)
   pos = pos or #list
   count = count or 1
   return
      (
         function (...)
            table.move(list, pos + count, #list + count, pos)
            return ...
         end
      )
      (
         table.unpack(list, pos, pos + count - 1)
      )
end

Lua VM limitation (250 registers) does not affect Lua function vararg length.
The maximal number of values table.remove_count() can return is almost the same as for any C API function such as table.unpack(); it is about half-million on my system.