Software /
code /
prosody
Changeset
5582:7bc2009fdd0c
util.iterators: Add filter() to run results through a filter function
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Fri, 17 May 2013 08:31:03 +0100 |
parents | 5581:167c1da54606 |
children | 5583:78dbece77ce8 |
files | util/iterators.lua |
diffstat | 1 files changed, 15 insertions(+), 1 deletions(-) [+] |
line wrap: on
line diff
--- a/util/iterators.lua Fri May 17 08:30:28 2013 +0100 +++ b/util/iterators.lua Fri May 17 08:31:03 2013 +0100 @@ -123,7 +123,21 @@ 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