Skip to content

Instantly share code, notes, and snippets.

@Anaminus
Created July 1, 2020 23:39
Show Gist options
  • Select an option

  • Save Anaminus/61a21dcf339f90d7b439597ff587b350 to your computer and use it in GitHub Desktop.

Select an option

Save Anaminus/61a21dcf339f90d7b439597ff587b350 to your computer and use it in GitHub Desktop.
Make a table an iterable array.
local t = {'a', 'b', 'c', 'd'}
setmetatable(t, {
__call = function(t, _, i)
if i == nil then
i = 1
else
i = i + 1
end
local v = t[i]
if v == nil then
return nil
end
return i, v
end,
})
for i, v in t do
print(i, v)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment