diff options
author | Matthew Wild <mwild1@gmail.com> | 2013-05-17 08:31:03 +0100 |
---|---|---|
committer | Matthew Wild <mwild1@gmail.com> | 2013-05-17 08:31:03 +0100 |
commit | d7f5ff243ecc816cad86b5e49ec4ad7c2ceaf83d (patch) | |
tree | b3c8bfbf9955ee2605e7ddcd76bfe81bd16700d2 /util/iterators.lua | |
parent | 61ccc4463ded21290bd964409597bbade9781108 (diff) | |
download | prosody-d7f5ff243ecc816cad86b5e49ec4ad7c2ceaf83d.tar.gz prosody-d7f5ff243ecc816cad86b5e49ec4ad7c2ceaf83d.zip |
util.iterators: Add filter() to run results through a filter function
Diffstat (limited to 'util/iterators.lua')
-rw-r--r-- | util/iterators.lua | 16 |
1 files changed, 15 insertions, 1 deletions
diff --git a/util/iterators.lua b/util/iterators.lua index 8f521fc4..bcda48b4 100644 --- a/util/iterators.lua +++ b/util/iterators.lua @@ -123,7 +123,21 @@ function it.tail(n, f, s, var) local ret = results[((count-1+pos)%n)+1]; return unpack(ret, 1, ret.n); end - --return reverse(head(n, reverse(f, s, var))); + --return reverse(head(n, reverse(f, s, var))); -- ! +end + +function it.filter(filter, f, s, var) + if type(filter) ~= "function" then + local filter_value = filter; + function filter(x) return x ~= filter_value; end + end + return function (s, var) + local ret; + repeat ret = pack(f(s, var)); + var = ret[1]; + until var == nil or filter(unpack(ret, 1, ret.n)); + return unpack(ret, 1, ret.n); + end, s, var; end local function _ripairs_iter(t, key) if key > 1 then return key-1, t[key-1]; end end |