Software /
code /
prosody
Comparison
util/iterators.lua @ 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 |
parent | 5581:167c1da54606 |
child | 5583:78dbece77ce8 |
comparison
equal
deleted
inserted
replaced
5581:167c1da54606 | 5582:7bc2009fdd0c |
---|---|
121 pos = pos + 1; | 121 pos = pos + 1; |
122 if pos > n then return nil; end | 122 if pos > n then return nil; end |
123 local ret = results[((count-1+pos)%n)+1]; | 123 local ret = results[((count-1+pos)%n)+1]; |
124 return unpack(ret, 1, ret.n); | 124 return unpack(ret, 1, ret.n); |
125 end | 125 end |
126 --return reverse(head(n, reverse(f, s, var))); | 126 --return reverse(head(n, reverse(f, s, var))); -- ! |
127 end | |
128 | |
129 function it.filter(filter, f, s, var) | |
130 if type(filter) ~= "function" then | |
131 local filter_value = filter; | |
132 function filter(x) return x ~= filter_value; end | |
133 end | |
134 return function (s, var) | |
135 local ret; | |
136 repeat ret = pack(f(s, var)); | |
137 var = ret[1]; | |
138 until var == nil or filter(unpack(ret, 1, ret.n)); | |
139 return unpack(ret, 1, ret.n); | |
140 end, s, var; | |
127 end | 141 end |
128 | 142 |
129 local function _ripairs_iter(t, key) if key > 1 then return key-1, t[key-1]; end end | 143 local function _ripairs_iter(t, key) if key > 1 then return key-1, t[key-1]; end end |
130 function it.ripairs(t) | 144 function it.ripairs(t) |
131 return _ripairs_iter, t, #t+1; | 145 return _ripairs_iter, t, #t+1; |