In Lua, how to get the tail of an array without copying it?
snippet in lua

In Lua, how to get the tail of an array without copying it?

user4571

function tail(t)
  return setmetatable({}, {
    __index = function(_, k) return t[k+1] end,
    __newindex = function(_, k, v) t[k+1] = v end,
    __len = function(_) return #t-1 end,
    __ipairs = function(_) return 
      function(_, i)
        if i+1==#t then return nil end
        return i+1, t[i+2] end, 
      t, 0 end,
    __pairs = function(t) return ipairs(t) end,
  })
end