Well, I don't know how "true" coroutines are defined, but Parrot, as
it's CPS based, has no problems with coroutines and there are no
restrictions to coroutines, AFAIK.


To be honest, I hadn't thought of this, either (this "true"-ness of coroutines), but then again, I'm no expert on these things.

How coroutines finally really behave WRT argument passing isn't really
layed out. There is a good article in Dan's blog IIRC.


I'll read it again.



co = coroutine.create(function ()
for i=1,10 do
print("co", i)
coroutine.yield()
end
end)



In Parrot's enough to include a .yield() ...

,--[ simplest usage ]-
| $ cat coro.imc
| .sub main @MAIN
|   $I0 = coro()
|   print $I0
|   $I0 = coro()
|   print $I0
| .end
| .sub coro
|   .yield(4)
|   .yield(2)
| .end
|
| $ ./parrot coro.imc
| 42
`---------------------



I hadn't seen ".yield(x)"
Is
   .yield(x)

the same as:

   .pcc_begin_yield
   .return x
   .pcc_end_yield
?

to get a static coroutine. Above Lua snippet would use the C<newsub>
opcode to create a Coroutine function object. Such objects act as
closures too (modulo untested, unimplemented features ;)



So, correct me if I'm wrong, a Coroutine is also a closure?
In that case, anytime you would create a closure, you could also create a coroutine?


I've also included the reply I got from Roberto:
<quote>

Untested code:
--------------------------------------------------------
function f(x)
A = function () x=x+1 end


B = coroutine.wrap(
function (y)
C = coroutine.wrap(
function (z)
while true do coroutine.yield(x+y+z) end
end)
while true do
y=y+1
coroutine.yield()
end
end)
end


X = f()
----------------------------------------------------------
Now we have:

- function A: each call increments x (in the main thread)

- coroutine B: each call increments y (in that coroutine)

- coroutine C: each call returns x+y+z (each in a different coroutine)


Of course, all this wold be more fun with recursion :) -- Roberto


</quote>

Do you think the above code snippet could work? That is, without much special code, can this be done in PIR?
(Of course, why would one ever want to write /such/ code :-P, but that's another issue)


Thanks,
Klaas-Jan




Reply via email to