Last active
June 22, 2020 13:36
-
-
Save gilzoide/97e4ce00fe6f0a4e21ee5c3bb0b4a12c to your computer and use it in GitHub Desktop.
A version of the `pairs` Lua function that ignores numeric keys
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 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