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 | 04ec0856a81726f5e0f67dc3c8e34c5f5027e06e (patch) | |
tree | b3c8bfbf9955ee2605e7ddcd76bfe81bd16700d2 /util | |
parent | 0672998fb8303d86ee3efaea7c8084ba5e01d563 (diff) | |
download | prosody-04ec0856a81726f5e0f67dc3c8e34c5f5027e06e.tar.gz prosody-04ec0856a81726f5e0f67dc3c8e34c5f5027e06e.zip |
util.iterators: Add filter() to run results through a filter function
Diffstat (limited to 'util')
-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 |