lua 作为迭代器的协程:无法恢复死协程
lua coroutine as iterator: cannot resume dead coroutine
那里,
我修改了 Lua 5.0 在线文档中的 "perm" 示例:http://www.lua.org/pil/9.3.html。我所做的是将 __call() 元方法重新指向 perm() 函数。但是只作用一次,报"cannot resume dead coroutine"。知道为什么它不起作用吗?
function permgen (a, n)
if n == 0 then
coroutine.yield(a)
else
for i=1,n do
-- put i-th element as the last one
a[n], a[i] = a[i], a[n]
-- generate all permutations of the other elements
permgen(a, n - 1)
-- restore i-th element
a[n], a[i] = a[i], a[n]
end
end
end
function perm (a)
local n = table.getn(a)
return coroutine.wrap(function () permgen(a, n) end)
end
K = {"a","b","c"}
for p in perm(K) do
print(p[1],p[2],p[3])
end
for p in perm(K) do
print(p[1],p[2],p[3])
end
-- everything above is copied from the Lua online document,
-- my modification is the following
setmetatable(K,{__call=perm(K)})
for p in K do
print(p[1],p[2],p[3])
end
-- cannot repeat!
-- perm.lua:44: cannot resume dead coroutine
for p in K do
print(p[1],p[2],p[3])
end
`
这是因为您调用了一次 perm(K)
并将结果分配给 __call
元方法。然后,您 使用 它一次(通过执行 in K
),这就完成了 perm
调用返回的协程的执行。第二次尝试时,协程已经"dead",触发错误
你需要做的是检测协程是否已经死亡并重新创建它。由于无法使用 coroutine.wrap
执行此操作,因此您需要使用 coroutine.create
对解决方案进行稍微修改的版本。这样的事情可能会奏效:
function perm (a)
local n = table.getn(a)
local co = coroutine.create(function () permgen(a, n) end)
return function () -- iterator
if coroutine.status(co) == 'dead' then co = coroutine.create(function () permgen(a, n) end) end
local code, res = coroutine.resume(co)
if not code then return nil end
return res
end
end
它会在恢复之前检查协程的状态,如果它已经 dead
,那么它会使用相同的参数从头开始重新创建它。
那里,
我修改了 Lua 5.0 在线文档中的 "perm" 示例:http://www.lua.org/pil/9.3.html。我所做的是将 __call() 元方法重新指向 perm() 函数。但是只作用一次,报"cannot resume dead coroutine"。知道为什么它不起作用吗?
function permgen (a, n)
if n == 0 then
coroutine.yield(a)
else
for i=1,n do
-- put i-th element as the last one
a[n], a[i] = a[i], a[n]
-- generate all permutations of the other elements
permgen(a, n - 1)
-- restore i-th element
a[n], a[i] = a[i], a[n]
end
end
end
function perm (a)
local n = table.getn(a)
return coroutine.wrap(function () permgen(a, n) end)
end
K = {"a","b","c"}
for p in perm(K) do
print(p[1],p[2],p[3])
end
for p in perm(K) do
print(p[1],p[2],p[3])
end
-- everything above is copied from the Lua online document,
-- my modification is the following
setmetatable(K,{__call=perm(K)})
for p in K do
print(p[1],p[2],p[3])
end
-- cannot repeat!
-- perm.lua:44: cannot resume dead coroutine
for p in K do
print(p[1],p[2],p[3])
end
`
这是因为您调用了一次 perm(K)
并将结果分配给 __call
元方法。然后,您 使用 它一次(通过执行 in K
),这就完成了 perm
调用返回的协程的执行。第二次尝试时,协程已经"dead",触发错误
你需要做的是检测协程是否已经死亡并重新创建它。由于无法使用 coroutine.wrap
执行此操作,因此您需要使用 coroutine.create
对解决方案进行稍微修改的版本。这样的事情可能会奏效:
function perm (a)
local n = table.getn(a)
local co = coroutine.create(function () permgen(a, n) end)
return function () -- iterator
if coroutine.status(co) == 'dead' then co = coroutine.create(function () permgen(a, n) end) end
local code, res = coroutine.resume(co)
if not code then return nil end
return res
end
end
它会在恢复之前检查协程的状态,如果它已经 dead
,那么它会使用相同的参数从头开始重新创建它。