- Last update
- Version History
This module is inspired by
Roshan James' "call/cc based implementation of Yield the Magnificent for Scheme".
Iterators are implemented with the macro lambda-yield and accessed with the macro iterate. Both are unhygienic by design.
They are then packaged into a coroutine record.
- iterators #!optional symprocedure
documentation-procedure; returns the list of exported symbols if called with no symbol, otherwise the signature of that exported symbol sym.
- (lambda-yield args xpr . xprs)syntax
A curried lambda, which can yield a value in its body. Not hygienic because of yield.
- (define-iterator (name . args) xpr . xprs)syntax
Defines a procedure, name, with lambda-yield instead of lambda. Not hygienic because of yield.
- (iterate iterator xpr . xprs)syntax
invokes xpr . xprs whenever the iterator yields. Not hygienic, exports 'break' and 'it'; 'it' is the result of a yield and 'break' an escape-procedure.
- (yield-all iterator)syntax
yield all values of an interator, equivalent to (iterate iterator (yield it)), not hygienic because of yield.
A coroutine wraps an iterator into a coroutine object.
- coroutine iteratorprocedure
packages the iterator, i.e. a unary procedure of the yield argument, into a coroutine and does a first move, so that the coroutine object contains an escape-procedure
- coroutine? xprprocedure
- co-move coprocedure
iterates internal iterator till it yields or till it returns; returns new coroutine
- co-value coprocedure
returns the value of a coroutine.
- co-finished? coprocedure
has coroutine finished execution?
- co-not-finished? coprocedure
is coroutine's iterator still running?
- co-return val coprocedure
sets a return value into the coroutine and returns the new coroutine. This return value can be yielded by the internal iterator
- coroutines #!rest itersprocedure
equivalent to (map coroutine iters), hence returns many coroutines.
- co-all-finished? cosprocedure
did all internal iterators finish?
- co-any-finished? cosprocedure
did some internal iterator finish?
- co-none-finished? cosprocedure
are all internal iterators still running?
- co-values cosprocedure
equivalent to (map co-value cos).
- co-move-all cosprocedure
equivalent to (map co-move cos).
- co-return-all ret cosprocedure
equivalent to (map (lambda (co) (co-return ret co)) cos).
(import iterators) (define-iterator (evens) (let loop ((i 0)) (loop (+ (yield i) 2)))) (iterate (evens) (print "it = " it) (if (>= it 20) (break #f) it)) ;;; tree-walking (define-iterator (walk-tree tree) (cond ((pair? tree) (yield-all (walk-tree (car tree))) (yield-all (walk-tree (cdr tree)))) ((null? tree) '()) (else (yield tree)))) (iterate (walk-tree '(1 2 3 (5 6 7) (4 5) 8)) (print "value " it))) (define (same-fringe? t1 t2) (let loop ((c1 (coroutine (walk-tree t1))) (c2 (coroutine (walk-tree t2)))) ;(print "XXX " (co-value c1) " " (co-value c2)) (if (and (co-finished? c1) (co-finished? c2)) #t (if (or (co-finished? c1) (co-finished? c2)) #f (if (eq? (co-value c1) (co-value c2)) (loop (co-move c1) (co-move c2)) #f))))) (define (Same-fringe? t1 t2) (let loop ((cs (coroutines (walk-tree t1) (walk-tree t2)))) ;(print "YYY " (co-values cs)) (cond ((co-all-finished? cs) #t) ((co-any-finished? cs) #f) ((eq? (co-value (car cs)) (co-value (cadr cs))) (loop (co-move-all cs))) (else #f)))) (define-iterator (ping) (let loop () (yield 'ping) (loop))) (define-iterator (pong) (let loop () (yield 'pong) (loop))) (define (ping-pong n) (let loop ((k 0) (ci (coroutine (ping))) (co (coroutine (pong)))) (cond ((= k n) (print 'Finish)) ((< k n) (print (co-value ci) " " (co-value co)) (loop (+ k 1) (co-move ci) (co-move co)))))) (ping-pong 20)
Oct 11, 2019
This egg is hosted on the CHICKEN Subversion repository:
If you want to check out the source code repository of this egg and you are not familiar with Subversion, see this page.
Copyright (c) 2019, Juergen Lorenz All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. Neither the name of the author nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- initial version