github.com/arnodel/golua@v0.0.0-20230215163904-e0b5347eaaa1/lib/base/lua/ipairs.lua (about)

     1  local function ipairs_iterator(t, n)
     2      if n < #t then
     3          return n + 1, t[n + 1]
     4      end
     5  end
     6  
     7  local function _ipairs(t)
     8      return ipairs_iterator, t, 0
     9  end
    10  
    11  local t = {5, 4, 3}
    12  local s = 0
    13  
    14  for i, v in _ipairs(t) do
    15      s = s + i * v
    16  end
    17  print(s) -- 5*1 + 4*2 + 3*3
    18  --> =22
    19  
    20  s = 0
    21  for i, v in ipairs(t) do
    22      print(i, v)
    23      s = s + i * v
    24  end
    25  --> =1	5
    26  --> =2	4
    27  --> =3	3
    28  
    29  print(s)
    30  --> =22