1. iterators
    1. Iterators
      1. iterators
      2. lambda-yield
      3. define-iterator
      4. iterate
      5. yield-all
    2. Coroutines
      1. coroutine
      2. coroutine?
      3. co-move
      4. co-value
      5. co-finished?
      6. co-not-finished?
      7. co-return
      8. coroutines
      9. co-all-finished?
      10. co-any-finished?
      11. co-none-finished?
      12. co-values
      13. co-move-all
      14. co-return-all
    3. Requirements
    4. Example
  2. Last update
  3. Author
  4. License
  5. Version History

iterators

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

iterators

[procedure] (iterators [sym])

documentation-procedure; returns the list of exported symbols if called with no symbol, otherwise the signature of that exported symbol sym.

lambda-yield

[syntax] (lambda-yield args xpr . xprs)

A curried lambda, which can yield a value in its body. Not hygienic because of yield.

define-iterator

[syntax] (define-iterator (name . args) xpr . xprs)

Defines a procedure, name, with lambda-yield instead of lambda. Not hygienic because of yield.

iterate

[syntax] (iterate iterator xpr . xprs)

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

[syntax] (yield-all iterator)

yield all values of an interator, equivalent to (iterate iterator (yield it)), not hygienic because of yield.

Coroutines

A coroutine wraps an iterator into a coroutine object.

coroutine

[procedure] (coroutine iterator)

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?

[procedure] (coroutine? xpr)

Type predicate.

co-move

[procedure] (co-move co)

iterates internal iterator till it yields or till it returns; returns new coroutine

co-value

[procedure] (co-value co)

returns the value of a coroutine.

co-finished?

[procedure] (co-finished? co)

has coroutine finished execution?

co-not-finished?

[procedure] (co-not-finished? co)

is coroutine's iterator still running?

co-return

[procedure] (co-return val co)

sets a return value into the coroutine and returns the new coroutine. This return value can be yielded by the internal iterator

coroutines

[procedure] (coroutines . iters)

equivalent to (map coroutine iters), hence returns many coroutines.

co-all-finished?

[procedure] (co-all-finished? cos)

did all internal iterators finish?

co-any-finished?

[procedure] (co-any-finished? cos)

did some internal iterator finish?

co-none-finished?

[procedure] (co-none-finished? cos)

are all internal iterators still running?

co-values

[procedure] (co-values cos)

equivalent to (map co-value cos).

co-move-all

[procedure] (co-move-all cos)

equivalent to (map co-move cos).

co-return-all

[procedure] (co-return-all ret cos)

equivalent to (map (lambda (co) (co-return ret co)) cos).

Requirements

none

Example


(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)

Last update

Oct 11, 2019

Author

Juergen Lorenz

License

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.

Version History

0.1
initial version