github.com/hirochachacha/plua@v0.0.0-20170217012138-c82f520cc725/stdlib/base/testdata/ipairs.lua (about)

     1  f, t, k =  ipairs({3, 4, 5})
     2  assert(k == 0)
     3  k, v = f(t, k)
     4  assert(k == 1 and v == 3)
     5  k, v = f(t, k)
     6  assert(k == 2 and v == 4)
     7  k, v = f(t, k)
     8  assert(k == 3 and v == 5)
     9  assert(f(t, k) == nil)
    10  
    11  local t = setmetatable({}, {__index = function(t, k)
    12  	if k < 5 then
    13  		return 1
    14  	end
    15  end})
    16  
    17  local i = 0
    18  for k, v in ipairs(t) do
    19  	i = i + 1
    20  	assert(i == k and v == 1)
    21  end
    22  
    23  local t = setmetatable({}, {__ipairs = function(t)
    24  	return function(t, k)
    25  		if k == nil then
    26  			return 1, 2
    27  		end
    28  		if k < 3 then
    29  			return k+1, k+2
    30  		end
    31  	end, t, nil
    32  end})
    33  
    34  f, t, k = ipairs(t)
    35  assert(k == nil)
    36  k, v = f(t, k)
    37  assert(k == 1 and v == 2)
    38  k, v = f(t, k)
    39  assert(k == 2 and v == 3)
    40  k, v = f(t, k)
    41  assert(k == 3 and v == 4)