티스토리 뷰

반응형
--consumer-driven design
function receive()
local status, value = coroutine.resume(producer)
return value
end

function send(x)
coroutine.yield(x)
end

producer = coroutine.create(
function ()
while true do
local x = io.read()
send(x)
end
end)

consumer = function ()
while true do
local x = receive()
print(x)
end
end

consumer()
 
--fibonacci
co = coroutine.create(function(n, lhs, rhs)
while true do
n = lhs + rhs
rhs, lhs = lhs, n
print(n)
coroutine.yield()
end
end)

print 'enter a number'

local r, n = io.read('*n')
for i=3,r do coroutine.resume(co, n, 1, 1) end

 

오늘은 코루틴을 공부했습니다. 처음보는 개념인데 그렇게 어렵진 않아서 재미있네요. :9

반응형

'프로그래밍 > Lua' 카테고리의 다른 글

[프로그래밍 루아] 논리 연산자  (0) 2017.10.30
[프로그래밍 루아] type과 nil  (0) 2017.10.30
Comments