ruft für jeden Durchgang die <function> auf, bis diese nichts mehr zurückgibt.
Man kann sich also einen eigenen Iterator definieren, indem man diese Function zurückgibt.
Hier ein kleines Beispiel:
Code:
function range(i1, i2, increment)
local from = i2 and i1 or 1
local to = i2 and i2 or i1
local increment = increment or 1
local i = from - increment
return function()
i = i + increment
if i < to then
return i
end
end
end
for i in range(10, 20) do
print(i)
end
function range(i1, i2, increment)
local from = i2 and i1 or 1
local to = i2 and i2 or i1
local increment = increment or 1
local i = from - increment
return function()
i = i + increment
if i < to then
return i
end
end
end
for i in range(10, 20) do
print(i)
end
PHP Code:
for i = 10, 20 do
print(i)
end
Es geht ums Prinzip, wie Iteratoren in Lua funktionieren, nicht darum wie sinnvoll mein Beispiel ist.
Du kannst mit der Info aus diesem Thread auch viel komplexere Iteratoren basteln
function fibonacci_iter(max_num)
local before = -1
local key = -1
local curerent = 1
return function()
value = before + curerent
if value > max_num then
return nil, nil
end
before = curerent
curerent = value
key = key+1
return key, value
end
end
for k,v in fibonacci_iter(100) do
print(string.format("key=%2d, value=%2d", k, v))
end
Es handelt sich hierbei um die Fibonacci-Folge. (Die Summe zweier aufeinanderfolgender Zahlen ist immer die darauffolgende Zahl)
Es gilt immer F₀ = 0 und F₁ = 1!
Danach läuft das ganze nach Fₓ₊₁=Fₓ+Fₓ₋₁ //Gibt leider kein tiefgestelles Unicode n
Wieso ich das jetzt hier dazu geschrieben habe, weiß ich selber nicht ganz genau, jedoch sollte es zeigen, dass man doch mehr Zeugs mit Custom-Iteratoren anstellen kann als so mancher hier denkt.
Noch ein bisschen Offtopic am Ende:
i cry every time.
[GUIDE] Item Filter Guide | Cabal Crafting & Dungeon Guide by Dewa Gempak. 06/15/2008 - Cabal Guides & Templates - 5 Replies Since its his work, just SHARING it here, i just gonna link his URL from his work. because i dont like copy and pasting other ppl's guides:
http://119.110.98.150:90/ipb/index.php?showtopic= 1145
Q#1: Does it work with CabalRider (PH)?
A#1: Yes, it works, i tried it personally myself.
Q#2: I'm too dumb to understand it, how to make it work.
A#2: Try reading it again and again, if u still don't know how, don't use it then sleep.gif