首页 > 其他 > 详细

lua coroutine filter

时间:2014-02-13 15:24:38      阅读:319      评论:0      收藏:0      [点我收藏+]
function send (x)
	coroutine.yield(x)
end

function producer ()
	return coroutine.create (function ()
		while true do
			local x = io.read()
			send(x)
		end
	end)
end

function receive (prod)
	local status, value = coroutine.resume(prod)
	return value
end

function filter (prod)
	return coroutine.create(function ()
		for line = 1, math.huge do
			local x = receive(prod)
			x = string.format("%5d %s", line, x)
			send(x)
		end
	end)
end

function consumer (prod)
	while true do
		local x = receive(prod)
		io.write(x, "\n")
	end
end

p = producer()
f = filter(p)
consumer(f)

lua coroutine filter

原文:http://blog.csdn.net/kevin_samuel/article/details/19138553

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!