6.协程(2)
使用协同程序写出一个类似unix管道或stream io的结构
--输入流
function instream()
return coroutine.wrap(function()
while true do
--按行读取
local line=io.read("*l")
if line then
coroutine.yield(line)
else
break
end
end
end)
end
--对读取到的字符进行一些处理
function filter(ins)
return coroutine.wrap(function()
while true do
local line=ins()
if line then
line="**"..line.."**"
coroutine.yield(line)
else
break
end
end
end)
end
--输出流
function outstream(ins)
while true do
local line=ins()
if line then
print(line)
else
break
end
end
end
outstream(filter(instream()))
运行结果: