协同程序 过滤器

function receive (prod) --来接受数据 恢复生产者协同程序 等待他发送参数回来 local status, value = coroutine.resume(prod) return value end function send (x) --停止本协同程序 返回数据给 resume coroutine.yield(x) end function producer () --生产者协同程序 读取数据 发送给 调用 resume的函数 return coroutine.create(function () while true do local x = io.read() -- produce new value send(x) end end) end function filter (prod) --过滤器 return coroutine.create(function () local line = 1 while true do local x = receive(prod) -- get new value x = string.format("%5d %s", line, x) send(x) -- send it to consumer line = line + 1 end end) end function consumer (prod) --消费者协同程序 接受数据 写屏 接受的过滤过的数据 while true do local x = receive(prod) -- get new value io.write(x, "/n") -- consume new value end end p = producer() --创建生产数据的协同程序 f = filter(p) consumer(f) --or consumer(filter(producer()))  

你可能感兴趣的:(function,filter)