Description

Provides generator-like coroutine operations.

Author

Kon Lovett

Usage

(require-extension generator)

Download

generator.egg

Documentation

Routines supporting generator-like operations. For more general control operators see the F-operator egg.

Macros

macro: (make-coroutine ARGS EXPR ...)

Returns a coroutine procedure of ARGS. Use '(resume COROUTINE VALUE0 ...)' within the body to invoke another coroutine.

macro: (define-coroutine (NAME ARG ... [ARGLST]) EXPR ...)

Defines a procedure, NAME, that, when invoked, returns a coroutine procedure parameterized on 'ARG ...'. The ARGLST, if present, are the coroutine procedure arguments. Otherwise the coroutine procedure parameter is 'X'. Use '(resume COROUTINE VALUE0 ...)' within the body to invoke another coroutine.

macro: (make-generator EXPR ...)

Returns a generator. The '(yield VALUE)' procedure is used within the body 'EXPR ...' of the generator to produce a single value.

macro: (make-generator/values EXPR ...)

Returns a generator. The '(yield VALUE0 ...)' procedure is used within the body 'EXPR ...' of the generator to produce a multiple values.

macro: (define-generator (NAME ARG ...) EXPR ...)

Defines a procedure, NAME, that, when invoked, returns a generator parameterized on 'ARG ...'. The '(yield VALUE)' procedure is used within the body 'EXPR ...' of the generator to produce a single value.

macro: (define-generator/values (NAME ARG ...) EXPR ...)

Defines a procedure, NAME, that, when invoked, returns a generator parameterized on 'ARG ...'. The '(yield VALUE0 ...)' procedure is used within the body 'EXPR ...' of the generator to produce a multiple values.

Examples

(use generator)

(define-generator (list->iterator ls) (for-each yield ls) (yield 'EOL))
(define t (list->iterator '(1 2)))
(t)   ; => 1 continues at caller
(t)   ; => 2 continues at caller
(t)   ; => EOL

(define-generator (counter n) (yield n) (counter (add1 n)))
(define t (counter 20))
(t) ; => 20
(t) ; => 21
;... and so on

Version

License

Copyright (c) 2006, Kon Lovett.  All rights reserved.

Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the Software),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED ASIS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.