Simplify func.iterate

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

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

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