The so-called iterator is a mechanism that can traverse all elements in a collection. In Lua, The iterator is usually represented as a function, and the next element in the set is returned for each call.
1. The simplest iterator
First, let's look at the iterator in C ++ and output all elements in the vector.
#include <iostream>#include <vector>using namespace std;int main(){ vector<int> ivec(5,-1); for (vector<int>::iterator iter = ivec.begin(); iter!= ivec.end(); ++iter) cout<<*iter<<endl;return 1;}
The following is implemented using Lua.
--declare an iteratorfunction values(t)local i=0;return function() i=i+1;return t[i] endendt={-1,-1,-1,-1,-1}--create iteratoriter=values(t)while true dolocal element=iter()if element==nil then break endprint(element)end
The iterator defined in the program is the closure mentioned above.
This closure is usually designed with two functions: closure itself and a factory function used to create the closure.
It is easier to implement with generic for because it is live for this.
--for stylefor element in values(t) doprint(element)end
The results are the same.
2. Generic
If you want to use the iterator, you must create a closure, which is not required for generic. In fact, generic for stores three values: An iterator function, A constant state and a control variable.
Take a look at the previous generic for example:
days = {"Sunday", "Monday", "Tuesday", "Wednesday","Thursday", "Friday", "Saturday"}for k,v in pairs(days) do print(k..":"..v) end
The syntax of generic for is usually as follows:
for <var-list> in <exp-list> do <body>end
<Var-List> is a list of one or more variable names separated by commas. <exp-List> is a list of one or more expressions, which are also separated by commas, generally, the expression list has only one element, that is, a call to the iterator factory.
The first element in the Variable list is called "control variable". When it is nil, the loop ends.
The first thing for is to evaluate the Expressions Behind in. These expressions should return three values for storage: iterator functions, constant states, and initial values of control variables. This is similar to the assignment of multiple elements.
3. Stateless iterator
It refers to an iterator that does not save any status.
In each iteration, the for loop uses constant states and control variables to call iteration functions. A stateless iterator can use these two values to generate the next element for the next iteration.
Ipairs is such an iterator.
a={"one","two","three"}b={[2]="four",[3]="five",[4]="six"}for i,v in ipairs(a) doprint(i,v)endfor i,v in ipairs(b) doprint(i,v)end
The result shows that only a is printed, But B is not.
Here, the iteration state is the table to be traversed and the current index value.
When calling ipairs (A) of the For Loop, Lua obtains three values: ITER, constant State A, and control variable Initial Value 0, and then calls ITER (, 0), get 1, a [1]. In the second iteration, continue to call ITER (A, 1 ),
Get 2, a [2], and so on.
When traversing B, when key = 1, the value is nil, so the jump out of the loop directly does not output any value.