lua实现ipairs、pairs的功能

来源:互联网 发布:单片机常用代码 编辑:程序博客网 时间:2024/05/18 02:08
function list_iter (t)
  local i = 1
  local n = table.getn(t)
  local f
  local fs
  return function ()
    if i <= n then 
      if type(t[i])=="table" then 
        if f==nil then  f=list_iter(t[i]) end 
        fs=f()
        if fs then return fs else i=i+1 f=nil end 
      else  i = i + 1 return t[i-1] end
    end 
  end
end
t = {10, 20, 30,{50,60,70,{100,200,300}}}


for element in list_iter(t) do
  sysLog(element)
end
--迭代器通过判断自动调用自我返回子table的闭包
原创粉丝点击