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
17
func.lua
17
func.lua
@ -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(iterator(state, ...))
|
||||||
return loop(iterable(state, ...))
|
|
||||||
end
|
|
||||||
return loop(iterable(state, ...))
|
|
||||||
end
|
end
|
||||||
return _iterate(iterator(...))
|
return loop(iterator(state, ...))
|
||||||
end
|
end
|
||||||
|
|
||||||
function for_generator(caller, ...)
|
function for_generator(caller, ...)
|
||||||
|
2
test.lua
2
test.lua
@ -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}
|
||||||
|
Loading…
Reference in New Issue
Block a user