Simplify func.iterate

master
Lars Mueller 2021-08-08 23:32:50 +02:00
parent 28eeb4ccf8
commit e9865f0066
2 changed files with 8 additions and 11 deletions

View File

@ -35,17 +35,14 @@ function values(...)
return function() return unpack(args) end
end
-- Equivalent to `for x, y, z in iterator(...) do callback(x, y, z) end`
function iterate(callback, iterator, ...)
local function _iterate(iterable, state, ...)
local function loop(...)
if ... == nil then return end
callback(...)
return loop(iterable(state, ...))
end
return loop(iterable(state, ...))
-- Equivalent to `for x, y, z in iterator, state, ... do callback(x, y, z) end`
function iterate(callback, iterator, state, ...)
local function loop(...)
if ... == nil then return end
callback(...)
return loop(iterator(state, ...))
end
return _iterate(iterator(...))
return loop(iterator(state, ...))
end
function for_generator(caller, ...)

View File

@ -33,7 +33,7 @@ do
assert(tab[key] == value)
tab[key] = nil
end
func.iterate(check_entry, pairs, tab)
func.iterate(check_entry, pairs(tab))
assert(next(tab) == nil)
tab = {a = 1, b = 2}