Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

Re: Re (tilly) 1: Perl 6 coroutines (RFC 31)

by John M. Dlugosz (Monsignor)
on Jul 13, 2001 at 18:33 UTC ( #96411=note: print w/replies, xml ) Need Help??

in reply to Re (tilly) 1: Perl 6 coroutines (RFC 31)
in thread Perl 6 coroutines (RFC 31)

I've speculated about using "fibers" in Win32 to implement iterators that can yeild. Calling the function in its own super-lightweight thread means it can keep its stack, so it doesn't need to return but can yeild and keep looping. I never wrote up that article (yet) though.

You can also do iterator, today, using objects.

my $iterator= $container->iterate(); while (my $item= $iterator++) { process $$item; }
The code behind ++ would have to save its state and return each time, so it's harder to write than having a yeild. But you declare the iterator instance and can use that one (or another one).

What if yeild caused the call to return a token, and that is what you make subsequent calls on? You can have another one going without conflict.

I agree that it doesn't sound like co-routines as described in classic literature. Those would "call" each other, rather than having a yeild statement at all. Such co-routines as members of the same object would also make sense.


Replies are listed 'Best First'.
Re (tilly) 3: Perl 6 coroutines (RFC 31)
by tilly (Archbishop) on Jul 13, 2001 at 20:58 UTC
    Personally I don't think that the issue of avoiding conflict is that bad. If you really want multiple instances of the same thing, then just have a function that returns a closure, and each closure maintains its own state.

    Anyways the yield idea can work really nicely. As I said before, try Ruby. Ruby combines yield with a nice piece of syntactic sugar. In Ruby you can call functions 2 ways. In one way you just call the function. In the other you call the function and pass a block as an extra argument. When you do the latter then any yield will call the block. (You can also detect which way you are being called and function appropriately.)

    So what you get is effectively the same as creating a closure in your context and passing it to the function, and having it call the closure. However it works really nicely because, even though that is what you are doing, there is a lot less machinery needed to set it up. And it seems that people who have a hard time getting their heads around the idea, You encapsulate a closure and then pass it in have no problem with the idea of passing a block to a method named each and having your block called on each thing encapsulated in your object.

    As Larry Wall has noted, sometimes sugar is worth it in its own right. Even if you cannot readily use yield for the full generality of what you want, it can work really nicely.

    As I said before, you can play around with Ruby now to get a sense of what this feature will be like in Perl 6. (Of course many of the other features of Perl 6 are missing in Ruby...)

      Yes, I looked at Ruby when it was mentioned last month. I looked at the block thing compared with Perl.

      If Perl allows the implicit sub at any position rather than only the first, we can do the same thing like Ruby. Having yield will make some things easier, like the tree traversal.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://96411]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2020-10-26 02:12 GMT
Find Nodes?
    Voting Booth?
    My favourite web site is:

    Results (249 votes). Check out past polls.