Skip to content

Instantly share code, notes, and snippets.

@gilzoide
Last active June 22, 2020 13:36
Show Gist options
  • Save gilzoide/97e4ce00fe6f0a4e21ee5c3bb0b4a12c to your computer and use it in GitHub Desktop.
Save gilzoide/97e4ce00fe6f0a4e21ee5c3bb0b4a12c to your computer and use it in GitHub Desktop.
A version of the `pairs` Lua function that ignores numeric keys
-- Usage: for k, v in kpairs(t) do ... end
local function knext(t, index)
local value
repeat
index, value = next(t, index)
until type(index) ~= 'number'
return index, value
end
function kpairs(t)
return knext, t, nil
end
-- Coroutine version
-- Usage: for k, v in co_kpairs(t) do ... end
function co_kpairs(t)
return coroutine.wrap(function()
for k, v in pairs(t) do
if type(k) ~= 'number' then coroutine.yield(k, v) end
end
end)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment