浅沫记忆 发表于 2022-7-26 18:58:54

Lua有状态迭代器

#!/usr/bin/luaprint("haicoder()\n")
array = {"Hello", "HaiCoder", "Lua"}
function elementIterator (collection)
local index = 0
local count = #collection
-- 闭包函数
return function ()
index = index + 1
if index <= count
then
--返回迭代器的当前元素
return collection
end
end
end
for element in elementIterator(array)
do
print(string.format("Element = %s", element))
end

http://blog.itpub.net/69955379/viewspace-2907545/
页: [1]
查看完整版本: Lua有状态迭代器