mirror of
https://github.com/appgurueu/modlib.git
synced 2024-11-22 07:13:45 +01:00
Simplify func.iterate
This commit is contained in:
parent
28eeb4ccf8
commit
e9865f0066
11
func.lua
11
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, ...)
|
||||
-- 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(iterable(state, ...))
|
||||
return loop(iterator(state, ...))
|
||||
end
|
||||
return loop(iterable(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