# Lua迭代器

## 一般對於迭代器

array = {"Lua", "Tutorial"} for key,value in ipairs(array) do print(key, value) end

1 Lua
2 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

function square(iteratorMaxCount,currentNumber) if currentNumber<iteratorMaxCount then currentNumber = currentNumber+1 return currentNumber, currentNumber*currentNumber end end function squares(iteratorMaxCount) return square,iteratorMaxCount,0 end for i,n in squares(3) do print(i,n) end

1 1
2 4
3 9

## 迭代器狀態

array = {"Lua", "Tutorial"} function elementIterator (collection) local index = 0 local count = #collection -- The closure function is returned return function () index = index + 1 if index <= count then -- return the current element of the iterator return collection[index] end end end for element in elementIterator(array) do print(element) end

Lua
Tutorial