Skip to content

Instantly share code, notes, and snippets.

@tj
Last active August 29, 2015 13:55
Show Gist options
  • Save tj/8761361 to your computer and use it in GitHub Desktop.
Save tj/8761361 to your computer and use it in GitHub Desktop.
def receive(co)
let status, val = coroutine.resume(co)
return val
end
def send(x)
coroutine.yield(x)
end
def producer
return coroutine.create(def
loop
let x = io.read()
send(x)
end
end)
end
def prepend(str, co)
return coroutine.create(def
loop
let x = receive(co)
send(str .. x)
end
end)
end
def uppercase(co)
return coroutine.create(def
loop
let x = receive(co)
send(x:upper())
end
end)
end
def consumer(co)
loop
let x = receive(co)
io.write(x, '\n')
end
end
consumer(prepend('>>> ', uppercase(producer())))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment