Created
August 9, 2012 04:05
-
-
Save SegFaultAX/3300858 to your computer and use it in GitHub Desktop.
Any and All in Lua
This file contains hidden or 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
-- 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