foreach_ext
by MuffinMario
Wendet eine Function f()
auf alle Elemente in einer Tabelle t
an. Es können entsprechende zusätzliche Parameter mit übergeben werden für f()
function foreach_ext (t, f, ...)
local i, v = next(t, nil)
while i do
local args = arg
tinsert(args,1,v)
tinsert(args,1,i)
local res = call(f,args)
tremove(args,1); -- it is the same object hence remove it again
tremove(args,1);
if res then return res end
i, v = next(t, i)
end
end
Beispiel mit Parameterübergabe:
t = { 10,20,30 }
foreach_ext(t,function(i,v,msg) print(msg .. " " .. i .. " " ..v) end,"hello")
Last updated