3 ms·
This is somewhat inefficient but zero indexed strings can easily be simulated in lua: local array = function(backing) local res = { len = function() retu
by norir 3y ago
This is somewhat inefficient but zero indexed strings can easily be simulated in lua:
local array = function(backing)
local res = { len = function() return #backing end }
setmetatable(res, {
__index = function(_, k) return backing[k + 1] end,
__newindex = function(_, k, v) backing[k + 1] = v end
})
return res
end
local x = array { 1, 2, 3 }
print(x[0]) -- 1
x[1] = 5
x[x.len()] = 6
for i = 0, x.len() - 1 do print(x[i]) end -- 1\n5\n3\n6