在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
泛型 for 迭代器array = {"Lua", "Tutorial"} for key,value in ipairs(array) do print(key, value) end 输出 Lua
Tutorial
无状态的迭代器function square(iteratorMaxCount,currentNumber) if currentNumber<iteratorMaxCount then currentNumber = currentNumber+1 return currentNumber, currentNumber*currentNumber end end for i,n in square,3,0 do print(i,n) end 输出 1 1 2 4 3 9
多状态的迭代器array = {"Lua", "Tutorial"} function elementIterator (collection) local index = 0 local count = #collection -- 闭包函数 return function () index = index + 1 if index <= count then -- 返回迭代器的当前元素 return collection[index] end end end for element in elementIterator(array) do print(element) end 输出 Lua
Tutorial
|
请发表评论