Skip to content

Instantly share code, notes, and snippets.

@SegFaultAX
Created August 9, 2012 04:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SegFaultAX/3300858 to your computer and use it in GitHub Desktop.
Save SegFaultAX/3300858 to your computer and use it in GitHub Desktop.
Any and All in Lua
-- Return true if any element of the iterable is true (or false if the iterable
-- is empty). Note that all values other than nil and false are considered true.
--
-- ... - any number of values
--
-- Examples
--
-- any(false, false, true) -- true
-- any(false, false, false) -- false
-- any(true, true, true) -- true
function any(...)
local args = { ... }
for _, v in pairs(args) do
if v then
return true
end
end
return false
end
-- Return true if all elements of the iterable are true (or true if the iterable
-- is empty). Note that all values other than nil and false are considered true.
--
-- ... - any number of values
--
-- Examples
--
-- all(false, false, true) -- false
-- all(false, false, false) -- false
-- all(true, true, true) -- true
function all(...)
local args = { ... }
for _, v in pairs(args) do
if not v then
return false
end
end
return true
end
print(any(false, false, true)) -- true
print(any(false, false, false)) -- false
print(any(true, true, true)) -- true
print(all(false, false, true)) -- false
print(all(false, false, false)) -- false
print(all(true, true, true)) -- true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment