首页 > 代码库 > 闭包实现迭代器

闭包实现迭代器

function list_iter(t)    local i = 0    local n = table.getn(t)    return function()        i = i + 1        if i <= n then return t[i] end    endendt = {10, 20, 30}iter = list_iter(t)while true do    local element = iter()    if element == nil then break end    print(element)end

 

闭包实现迭代器