闭包实现迭代器
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了闭包实现迭代器相关的知识,希望对你有一定的参考价值。
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 end end t = {10, 20, 30} iter = list_iter(t) while true do local element = iter() if element == nil then break end print(element) end
以上是关于闭包实现迭代器的主要内容,如果未能解决你的问题,请参考以下文章