Lua - coroutines - official example

393 views Asked by At

on this website http://www.lua.org/pil/9.2.html there is an example of a coroutine.

I wrote the same example without coroutine, and I don't see any advantage of coroutines in the official example. Is there even an advantage of using coroutines on that example?

Same without coroutine (I replaced io.input with math.random)

local line = 1

function Produce ()
    local value  = math.random(10)
    return value
end

function Filter ()
    while true do
      local value = Produce()
      value       = string.format("%3d %s", line, value)    
      line        = line + 1
      return value
    end
end

function Consum ()
    while true do
        local value = Filter()
        print(value, "\n")     
    end
end

Consum()
0

There are 0 answers