local coroutine_poll ={}local coroutine_yield = coroutine.yield
localfunctionco_create(f)local co = table.remove(coroutine_poll)if co ==nilthen
co = coroutine.create(function(...)f(...)whiletruedo
f =nil
coroutine_poll[#coroutine_poll]= co
f = coroutine_yield "EXIT"f(coroutine_yield())endend)else
coroutine.resume(co, f)endreturn co
end
缓存函数计算结果
functionmemoize(f)local mem ={}setmetatable(mem,{ __mode ="kv"})returnfunction(x)local r = mem[x]if r ==nilthen
r =f(x)
mem[x]= r
endreturn r
endend
使用闭包代替loadstring()
loadstring(string.format("return %d", i))functionoploadstring(k)returnfunction()return k endend