Skip to content

Instantly share code, notes, and snippets.

@ifels
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ifels/a21a2decd6e0c1f1a352 to your computer and use it in GitHub Desktop.
Save ifels/a21a2decd6e0c1f1a352 to your computer and use it in GitHub Desktop.
lua pipe sample
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