mirror of
https://github.com/appgurueu/modlib.git
synced 2024-12-22 21:32:27 +01:00
Simplify func.iterate
This commit is contained in:
parent
28eeb4ccf8
commit
e9865f0066
17
func.lua
17
func.lua
@ -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, ...)
|
||||
|
2
test.lua
2
test.lua
@ -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}
|
||||
|
Loading…
Reference in New Issue
Block a user