用闭包实现ipairs

来源:互联网 发布:万方数据库论文查重 编辑:程序博客网 时间:2024/06/07 02:58
local table1 = {2,3,4,5,6}print("the first print:");for k,v in ipairs(table1) doprint(k,v)endfunction my_ipars(table)local key = 0return function()key = key+1return key,table[key]endendfunction My_ipars(table,callfuncN)local fun_ipars = my_ipars(table)while(1) do local key,value = fun_ipars();if value ~= nil thenif callfuncN ~= nil thencallfuncN(table,key,value)--回调机制endelsebreakendendendlocal callfuncN =function(table , key, value)print("the:",type(table)," the key:",key,"  the value:",value);table[key] = value * valueendprint("my ipairs print")My_ipars(table1,callfuncN)print("my second print")for i,v in ipairs(table1) doprint(i,v)end

0 0
原创粉丝点击