[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Filtering Iterator
- From: Kevin Martin <kev82@...>
- Date: Sat, 22 Dec 2012 15:45:57 +0000
Hi,
I need an iterator wrapper/decorator I can use with for that allows me to skip values that don't match a specified predicate. I've done a bit of searching, but haven't been able to find one. I've written the below code and it works for the simple example given, but I'm struggling to convince myself it's correct. Can anyone see anything wrong with it, or does it look ok?
Thanks,
Kevin
----------------------------------------------
function iterfilter(filter, iter, state1, var1)
return function(state2, var2)
while true do
local t = table.pack(iter(state2, var2))
if t[1] == nil then return nil end
if filter(table.unpack(t)) then
return table.unpack(t)
end
var2 = t[1]
end
end,
state1, var1
end
local t = {}
for k=1,100 do
t[k] = tostring(k)
end
local function div3(x)
return x % 3 == 0
end
local function div5(x)
return x % 5 == 0
end
for _,v in iterfilter(div3, iterfilter(div5, pairs(t))) do
print(v)
end