Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

By coroutine, he really means a language feature that converts:

    do_something_and_then(function (){
        do_another_thing_and_then( function (){
            say "we're done!"
        });
    });
to:

    do_something;
    do_another_thing;
    say "we're done";
Coroutines are one way of doing this, but source rewriting (Haskell-"do-notation"-style) also works.

An instructive example is the Coro module for Perl (which is a bit more coroutine-ish, as it provides separate perl and C stacks for each async action, etc.): http://search.cpan.org/perldoc?Coro

It lets you write something like:

    async {
        my $t = AnyEvent->timer( after => 5, cb => Coro::rouse_cb );
       Coro::rouse_wait();
       say "OH HAI";
    };
instead of:

    my $t = AnyEvent->timer( after => 5, cb => sub {
       say "OH HAI";
    });
  
This may look like it's not an improvement, but it is after you add some sugar:

    sub sleep($) {
        my $seconds = shift;
        my $cb = Coro::rouse_cb;
        my $t = AnyEvent->timer( after => $seconds, cb => $cb );
    }

    async {
        sleep 5;
        say "It's been 5 seconds!";
        sleep 10;
        say "It's been 15 seconds!";
    };
instead of:

    my $t; $t = AnyEvent->timer( after => 5, cb => sub {
        say "5";
        $t = AnyEvent->timer( after => 10, cb => sub {
            say "15";
            undef $t;
        });
    });


Consider applying for YC's Summer 2026 batch! Applications are open till May 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: