Latest web development tutorials

Lua iterator

Iterator (iterator) is an object that can be used to traverse the Standard Template Library container part or all of the elements, the address of each iterator object represents a container determined

In Lua iterator is a pointer type of support structure, which can traverse each element of the collection.


Generics for iterator

Generics for preservation in its own internal iterative function, in fact, it saves three values: an iterative function, state constants, control variables.

Generics for iteration provides a set of key / value pairs, syntax is as follows:

for k, v in pairs(t) do
    print(k, v)
end

The above code, k, v variable list; pair (t) for the list of expressions.

See the following examples:

array = {"Lua", "Tutorial"}

for key,value in ipairs(array) 
do
   print(key, value)
end

The output is the above code is executed:

1  Lua
2  Tutorial

The above examples we used the iterative function ipairs Lua provided by default.

Here we look at the process for the execution of Fan:

  • First, initialize, calculated in value after an expression, the expression should return for the required range of three values: an iterative function, state constants, control variables; and multi-value assignment, the number of results if the expression returns less than three with nil points will automatically make up, the extra part will be ignored.
  • Second, the state constants and control variable as a parameter to call the iterator function (Note: for the structure, the state constants useless, just get his values ​​when initialized and passed to the iteration function).
  • Thirdly, the function returns the value iteration to the variable list.
  • Fourth, if the return value of nil in the first end of the loop, otherwise the loop body.
  • Fifth, the call back to the second step iteration function again

. In Lua we often use to describe the iterator function, each call to this function returns the next element of the collection. Lua iterator contains the following two types:

  • Stateless iterator
  • Multi-state iterator

Stateless iterator

Stateless iterator refers does not retain any state iterators, so we can use the loop stateless iterator avoid creating closures spend extra price.

Each iteration, iteration functions are two variables (state and control variables constant) value as a parameter is called a stateless iterator using only these two values ​​to get the next element.

A typical example of such a simple stateless iterator is ipairs, he through each element of the array.

The following example we use a simple function to achieve the iterator, digital n squared:

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

The above example output is:

1	1
2	4
3	9

Iterative traversal state includes a table (during the cycle does not change the state constant) and the current index index (control variables), ipairs and iterative function is very simple, we can achieve in Lua:

function iter (a, i)
    i = i + 1
    local v = a[i]
    if v then
       return i, v
    end
end
 
function ipairs (a)
    return iter, a, 0
end

When Lua calls ipairs (a) to start the cycle, he acquired three values: an iterative function iter, a constant state, the control variable initial value of 0; then Lua calls iter (a, 0) returns 1, a [1] (unless a [1] = nil); the second iteration calls iter (a, 1) returns 2, a [2] ...... until the first nil element.


Multi-state iterator

In many cases, the need to preserve the plurality of the iterator state information rather than simply state control constants and variables, the easiest way is to use closures, there is a way to encapsulate all state information into a table, the table as an iterative the status of the constants, because in this case all the information can be stored in the table, so iterative function usually do not need the second parameter.

The following example we created our own iterators:

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

The above example output is:

Lua
Tutorial

The above examples we can see, the use of closures within elementIterator function, calculated to achieve the set size and output of each element.