Stateless Iterators

As the name implies, a stateless iterator is an iterator that does not keep any state by itself. Therefore, we may use the same stateless iterator in multiple loops, avoiding the cost of creating new closures.

On each iteration, the for loop calls its iterator function with two arguments: the invariant state and the control variable. A stateless iterator generates the next element for the iteration using only these two arguments. A typical example of this kind of iterator is ipairs, which iterates over all elements in an array, as illustrated next:

    a = {"one", "two", "three"}
    for i, v in ipairs(a) do
      print(i, v)
    end

The state of the iteration is the table being traversed (the invariant state, which does not change during the loop), plus the current index (the control variable). Both ipairs and the iterator it returns are quite simple; we could write them in Lua as follows:

    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) in a for loop, it gets three values: the iter function as the iterator, a as the invariant state, and zero as the initial value for the control variable. Then, Lua calls iter(a, 0), which results in 1, a[1] (unless a[1] is already nil). In the second iteration, it calls iter(a, 1), which results in 2, a[2], and so on, until the first nil element.

The pairs function, which iterates over all elements in a table, is similar, except that the iterator function is the next function, which is a primitive function in Lua:

    function pairs (t)
      return next, t, nil
    end

The call next(t, k), where k is a key of the table t, returns a next key in the table, in an arbitrary order. (It returns also the value associated with that key, as a second return value.) The call next(t, nil) returns a first pair. When there are no more pairs, next returns nil.

Some people prefer to use next directly, without calling pairs:

    for k, v in next, t do
      ...
    end

Remember that the expression list of the for loop is adjusted to three results, so Lua gets nextt, and nil, exactly what it gets when it calls pairs(t).

标准库提供了集中迭代器,包括迭代文件每行的(io.lines),迭代table元素的(pairs),迭代数组元素的(ipairs),迭代字符串中单词的 

(string.gmatch)等等。LUA手册中对与pairs,ipairs解释如下:

ipairs (t)

Returns three values: an iterator function, the table t, and 0, so that the construction

for i,v in ipairs(t) do body end

will iterate over the pairs (1,t[1]), (2,t[2]), ···, up to the first integer key absent from the table.

pairs (t)

Returns three values: the next function, the table t, and nil, so that the construction

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

will iterate over all key–value pairs of table t.

See function next for the caveats of modifying the table during its traversal.

这样就可以看出  ipairs以及pairs 的不同。

pairs可以遍历表中所有的key,并且除了迭代器本身以及遍历表本身还可以返回nil;

但是ipairs则不能返回nil,只能返回数字0,如果遇到nil则退出。它只能遍历到表中出现的第一个不是整数的key

 1 下面举个例子吧!
 2  
 3  eg:
 4 local tabFiles = {
 5 [3] = "test2",
 6 [6] = "test3",
 7 [4] = "test1"
 8 }
 9  
10 for k, v in ipairs(tabFiles) do
11 print(k, v)
12 end
13  
14  
15 猜测它的输出结果是什么呢?
16  
17 根据刚才的分析,它在 ipairs(tabFiles) 遍历中,当key=1时候value就是nil,所以直接跳出循环不输出任何值。
18  
19 >lua -e "io.stdout:setvbuf 'no'" "Test.lua"
20 >Exit code: 0
21  
22 那么,如果是
23 for k, v in pairs(tabFiles) do
24 print(k, v)
25 end
26 则会输出所有 :
27 >lua -e "io.stdout:setvbuf 'no'" "Test.lua" 
28 3 test2
29 6 test3
30 4 test1
31 >Exit code: 0
32 现在改变一下表内容,
33 local tabFiles = {
34 [1] = "test1",
35 [6] = "test2",
36 [4] = "test3"
37 }
38 for k, v in ipairs(tabFiles) do
39 print(k, v)
40 end
41 现在的输出结果显而易见就是key=1时的value值test1
42  >lua -e "io.stdout:setvbuf 'no'" "Test.lua" 
43 1 test1
44 >Exit code: 0 
45 --[示例1.]--
46 local tt =
47 {
48     [1] = "test3",
49     [4] = "test4",
50     [5] = "test5"
51 }
52 
53 for i,v in pairs(tt) do        -- 输出 "test4" "test3" "test5"
54     print( tt[i] )
55 end
56 
57 for i,v in ipairs(tt) do    -- 输出 "test3" k=2时断开
58     print( tt[i] )
59 end
60 
61 
62 
63 
64 
65 -- [[示例2.]] --
66 tbl = {"alpha", "beta", [3] = "uno", ["two"] = "dos"}
67 
68 for i,v in ipairs(tbl) do    --输出前三个
69     print( tbl[i] )
70 end
71 
72 for i,v in pairs(tbl) do    --全部输出
73     print( tbl[i] )
74 end
原文地址:https://www.cnblogs.com/reynold-lei/p/3570569.html