| 1 |
;;; |
| 2 |
;;; SRFI-221 - Generator/accumulator sub-library |
| 3 |
;;; |
| 4 |
;;; Copyright (c) 2021-2024 Shiro Kawai <[email protected]> |
| 5 |
;;; |
| 6 |
;;; Redistribution and use in source and binary forms, with or without |
| 7 |
;;; modification, are permitted provided that the following conditions |
| 8 |
;;; are met: |
| 9 |
;;; |
| 10 |
;;; 1. Redistributions of source code must retain the above copyright |
| 11 |
;;; notice, this list of conditions and the following disclaimer. |
| 12 |
;;; |
| 13 |
;;; 2. Redistributions in binary form must reproduce the above copyright |
| 14 |
;;; notice, this list of conditions and the following disclaimer in the |
| 15 |
;;; documentation and/or other materials provided with the distribution. |
| 16 |
;;; |
| 17 |
;;; 3. Neither the name of the authors nor the names of its contributors |
| 18 |
;;; may be used to endorse or promote products derived from this |
| 19 |
;;; software without specific prior written permission. |
| 20 |
;;; |
| 21 |
;;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 22 |
;;; "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 23 |
;;; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 24 |
;;; A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 25 |
;;; OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 26 |
;;; SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED |
| 27 |
;;; TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 28 |
;;; PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 29 |
;;; LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 30 |
;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 31 |
;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 32 |
;;; |
| 33 |
|
| 34 |
(define-module srfi.221 |
| 35 |
(use gauche.generator) |
| 36 |
(use scheme.vector :only (vector-every)) |
| 37 |
(use util.stream) |
| 38 |
(export gcompose-left gcompose-right |
| 39 |
accumulate-generated-values |
| 40 |
genumerate |
| 41 |
gchoice |
| 42 |
stream->generator |
| 43 |
generator->stream) |
| 44 |
) |
| 45 |
(select-module srfi.221) |
| 46 |
|
| 47 |
(define (gcompose-left ctor . ops) |
| 48 |
(let loop ([g (ctor)] [ops ops]) |
| 49 |
(if (null? ops) |
| 50 |
g |
| 51 |
(loop ((car ops) g) (cdr ops))))) |
| 52 |
|
| 53 |
(define (gcompose-right arg . ops&ctor) |
| 54 |
(if (null? ops&ctor) |
| 55 |
(arg) ;returns a generator |
| 56 |
(let1 ctor (last ops&ctor) |
| 57 |
(let rec ([ops (cons arg (drop-right ops&ctor 1))]) |
| 58 |
(if (null? ops) |
| 59 |
(ctor) |
| 60 |
((car ops) (rec (cdr ops)))))))) |
| 61 |
|
| 62 |
|
| 63 |
(define (accumulate-generated-values acc gen) |
| 64 |
(let loop ([v (gen)]) |
| 65 |
(if (eof-object? v) |
| 66 |
(acc v) ;returns accumulated value |
| 67 |
(begin (acc v) (loop (gen)))))) |
| 68 |
|
| 69 |
(define (genumerate gen) |
| 70 |
(define counter -1) |
| 71 |
(^[] (glet1 v (gen) |
| 72 |
(begin (inc! counter) |
| 73 |
(cons counter v))))) |
| 74 |
|
| 75 |
(define (gchoice choice-gen . source-gens) |
| 76 |
(let1 srcs (list->vector source-gens) |
| 77 |
(rec (gen) |
| 78 |
(glet1 c (choice-gen) |
| 79 |
(assume (and (exact-integer? c) |
| 80 |
(<= 0 c (- (vector-length srcs) 1)))) |
| 81 |
(let1 g (vector-ref srcs c) |
| 82 |
(if g |
| 83 |
(let1 v (g) |
| 84 |
(if (eof-object? v) |
| 85 |
(begin |
| 86 |
(vector-set! srcs c #f) |
| 87 |
(if (vector-every not srcs) |
| 88 |
(eof-object) |
| 89 |
(gen))) |
| 90 |
v)) |
| 91 |
(gen))))))) |
| 92 |
|
| 93 |
(define (stream->generator stream) |
| 94 |
(^[] (if (stream-null? stream) |
| 95 |
(eof-object) |
| 96 |
(begin0 (stream-car stream) |
| 97 |
(set! stream (stream-cdr stream)))))) |
| 98 |
|
| 99 |
(define (generator->stream gen) |
| 100 |
(stream-delay |
| 101 |
(let loop ([v (gen)]) |
| 102 |
(if (eof-object? v) |
| 103 |
stream-null |
| 104 |
(stream-cons v (loop (gen))))))) |