19.迭代器和泛型for_泛型


19.迭代器和泛型for_泛型_02

function values(t)
local i=0;
return function()
i=i+1;
return t[i];
end
end

t={10,20,30};
local iter=values(t);
while true do
local element=iter();
if element==nil then
break;
else
print(element);
end
end

--10
--20
--30

02.for

for element in values(t) do
print(element);
end

19.迭代器和泛型for_泛型_03


03.泛型for

19.迭代器和泛型for_泛型_04