Skip to content

Instantly share code, notes, and snippets.

@catwell
Last active April 9, 2016 18:55
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save catwell/9326770 to your computer and use it in GitHub Desktop.
Save catwell/9326770 to your computer and use it in GitHub Desktop.
-- my usual implementation
local is_posint = function(k)
return (type(k) == "number") and (k > 0) and (k % 1 == 0)
end
local is_array = function(t)
local n, nmax = 0, 0
for k,_ in pairs(t) do
if is_posint(k) then
if k > nmax then nmax = k end
else return false end
n = n+1
end
return n == nmax
end
-- from http://ericjmritz.name/2014/02/26/lua-is_array/
function is_array(t)
local i = 0
for _ in pairs(t) do
i = i + 1
if t[i] == nil then return false end
end
return true
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment