# Lua的Iterator以及函数心得

local tbl = {"one", "two", "three", ‘five’}
local count = #tbl
local inc = 0

function Iter(tableinput)
return function()
if inc > count then
return
end
inc = inc + 1
return tableinput[inc], inc
end
end

for elem, index in Iter(tbl) do
print(elem)
if index > 2 then
inc = 0
end
end

function Iter(tableinput)
local count2 = #tbl
local inc2 = 0

return function()
if inc2 > count2 then
return
end
inc2 = inc2 + 1
return tableinput[inc2], inc2
end
end

stringb = "bbbbbbbbbbbbbbb"
do
local internalA = "print for first time"
function printStrA()
print(internalA)
end
function printStrB()
print(stringb)
end
end

do
local internalA = "!!!!!!!!!!!!!!!!!!!!"
stringb = "cccccccccccccccc"
printStrA()
printStrB()
end

internalA = "!!!!2222222222222"
printStrA()
stringb = "ddddddddddddddddddd"
printStrB()

print for first time
cccccccccccccccc
print for first time
ddddddddddddddddddd

local x = function ()
local i = 0
print "aaa"
local function x2()
i = i + 1
print "bbb"
return i
end
return x2 — 这个地方不能是return x2()，否则返回的就是值而不是函数
end

x()
print "======="
x()
print "======="

local c = x()
print "======="
print(c())
print "======="
print(c())
print "======="
print(c())
print "======="

local c = x()
print "======="
print(c())

aaa
=======
aaa
=======
aaa
=======
bbb
1
=======
bbb
2
=======
bbb
3
=======
aaa
=======
bbb
1