Last active
August 29, 2015 14:01
-
-
Save ifels/a21a2decd6e0c1f1a352 to your computer and use it in GitHub Desktop.
lua pipe sample
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function receive (prod) | |
local status, value = coroutine.resume(prod) | |
return value | |
end | |
function send (x) | |
coroutine.yield(x) | |
end | |
function producer () | |
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 () | |
for line = 1, math.huge do | |
local x = receive(prod) -- get new value | |
x = string.format("%5d %s", line, x) | |
send(x) -- send it to consumer | |
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 | |
consumer(filter(producer())) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment