[plt-scheme] "Standard" coroutine library suitable for building filters as well as generators

From: Eli Barzilay (eli at barzilay.org)
Date: Mon Apr 5 15:14:59 EDT 2010

On Apr  5, Scott McLoughlin wrote:
> In addition to generators via yield, I'm also looking for coroutines
> that can receive values as well via a coroutine operator such as
> "receive".  Something like:
> 
> (define-coro (print-all)
>     (while #t
>         (let ((x (receive)))   ; receive is a keyword that blocks on an 
> expected "resume"
>            (print x)))))
> 
> (let ((pa (print-all)))   ; "instantiates" the coroutine, here with no 
> initialization arguments
>     (resume pa 4)         ; this pumps values to a pending "receive" 
> within the coroutine
>     (resume pa 2)
>     (resume pa 6))
> 
> 4
> 2
> 6

There's a new feature in 4.2.5 -- calling the generator can accept
arguments that are received in the generator as the result of the
`yield' call:

  #lang scheme
  (require scheme/generator)
  (define print-all
    (generator () ; (generator (let ...)) in 4.2.5
      (let loop () (printf "~s\n" (yield)) (loop))))
  (print-all)
  (print-all 4)
  (print-all 2)
  (print-all 6)

(This was not in the announcement, because of the syntax change which
is intended to make it possible to send initial arguments to the
generator.)

-- 
          ((lambda (x) (x x)) (lambda (x) (x x)))          Eli Barzilay:
                    http://barzilay.org/                   Maze is Life!


Posted on the users mailing list.