Created
September 26, 2012 19:59
-
-
Save FGRibreau/3790217 to your computer and use it in GitHub Desktop.
Lua table.filter (JavaScript Array::filter equivalent)
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
-- table.filter({"a", "b", "c", "d"}, function(o, k, i) return o >= "c" end) --> {"c","d"} | |
-- | |
-- @FGRibreau - Francois-Guillaume Ribreau | |
-- @Redsmin - A full-feature client for Redis http://redsmin.com | |
table.filter = function(t, filterIter) | |
local out = {} | |
for k, v in pairs(t) do | |
if filterIter(v, k, t) then out[k] = v end | |
end | |
return out | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To make the output a regular iterable array, you can replace :
out[k] = v
by
table.insert(out,v)