Lua chapter 6
时间:2014-05-10 08:36:53
收藏:0
阅读:268
一个简单的迭代器示例
--迭代工厂函数
function value(t)
local i = 0;
returnfunction()
i = i+1;
return t[i];
end;
end;t = {10,20,30};
iter = value(t);
while true do
local element = iter();
if element == nil then
break;
end;
print("Element: " .. element);end;
print();
for element in value(t) do
print("Element: " .. element);
end;
2、输出所有单词
function allword()local line = io.read();
local pos = 1;
return
function()
local s, e = string.find(line, "%w+", pos);if s then
pos = e + 1;
return string.sub(line, s, e);
end;
end;
end;
for word in allword() do
print(word);
end;
评论(0)