messages = {'hey!', 'listen!'}
closures = {}
for i = 1,10 do
local m = messages[math.random(#messages)]
closures[i] = function() print(m) end
end
for _, f in ipairs(closures) do
f()
end
function newCounter()
local i = 0
return function(inc)
i = i + (inc or 1)
return i
end
end
a, b = newCounter()
print(a(3)) --> 3
print(a(), b()) --> 4, 1
I have come here to chew bubblegum and kick ass... and I'm all out of bubblegum.
function newCounter()
local i = 0
return function(inc)
i = i + (inc or 1)
return i
end
end
a, b = newCounter()
print(a(3)) --> 3
print(a(), b()) --> 4, 1