lua-users home
lua-l archive

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


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