lua 迭代器
可以用lua语言自己创建一个迭代器
- 具体代码如下
代码块
代码块语法遵循标准markdown代码,例如:
function list_iter(t)
local i = 0
local n = table.getn(t)
return function ()
i = i + 1
if i <= n then return t[i] end
end
end
local helpful_guys = {
"----1----",
"buxiu", "zhang3",
"morler", "lambda", "sunlight",
"\n",
"----2----",
"doyle", "flicker",
"zhang3", "Kasi",
"\n"
}
for e in list_iter(helpful_guys) do
print(e)
end
输出结果
说明
其中list_iter函数就是自制的迭代器了。