| 1 |
;;; |
| 2 |
;;; scheme.generator - Generators and Accumulators (R7RS Tangerine) |
| 3 |
;;; |
| 4 |
;;; Copyright (c) 2020-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 |
;; Originally SRFI-158 |
| 35 |
;; (In R7RS Red edition, it was SRFI-121). |
| 36 |
;; Generator library is built-in. We provide accumulator implementation here. |
| 37 |
|
| 38 |
(define-module scheme.generator |
| 39 |
(use gauche.unicode) |
| 40 |
(use gauche.generator) |
| 41 |
(export generator circular-generator make-iota-generator make-range-generator |
| 42 |
make-coroutine-generator list->generator vector->generator |
| 43 |
reverse-vector->generator string->generator |
| 44 |
bytevector->generator |
| 45 |
make-for-each-generator make-unfold-generator |
| 46 |
gcons* gappend gflatten ggroup gmerge gmap gcombine gfilter gremove |
| 47 |
gstate-filter ggroup generator-map->list |
| 48 |
gtake gdrop gtake-while gdrop-while |
| 49 |
gdelete gdelete-neighbor-dups gindex gselect |
| 50 |
generator->list generator->reverse-list generator-map->list |
| 51 |
generator->vector generator->vector! generator->string |
| 52 |
generator-fold generator-for-each generator-find |
| 53 |
generator-count generator-any generator-every generator-unfold |
| 54 |
|
| 55 |
;; accumulators |
| 56 |
make-accumulator count-accumulator list-accumulator |
| 57 |
reverse-list-accumulator vector-accumulator |
| 58 |
reverse-vector-accumulator vector-accumulator! |
| 59 |
string-accumulator bytevector-accumulator bytevector-accumulator! |
| 60 |
sum-accumulator product-accumulator)) |
| 61 |
(select-module scheme.generator) |
| 62 |
|
| 63 |
(define (make-accumulator kons knil finalizer) |
| 64 |
(^v |
| 65 |
(if (eof-object? v) |
| 66 |
(finalizer knil) |
| 67 |
(begin (set! knil (kons v knil)) (undefined))))) |
| 68 |
|
| 69 |
(define (list-accumulator) |
| 70 |
(make-accumulator cons '() reverse)) |
| 71 |
|
| 72 |
(define (reverse-list-accumulator) |
| 73 |
(make-accumulator cons '() identity)) |
| 74 |
|
| 75 |
(define (vector-accumulator) |
| 76 |
(make-accumulator cons '() reverse-list->vector)) |
| 77 |
|
| 78 |
(define (reverse-vector-accumulator) |
| 79 |
(make-accumulator cons '() list->vector)) |
| 80 |
|
| 81 |
(define (vector-accumulator! vec at) |
| 82 |
(make-accumulator (^[v i] |
| 83 |
(when (>= i (vector-length vec)) |
| 84 |
(error "vector is full")) |
| 85 |
(vector-set! vec i v) |
| 86 |
(+ i 1)) |
| 87 |
at |
| 88 |
(^_ vec))) |
| 89 |
|
| 90 |
(define (string-accumulator) |
| 91 |
(make-accumulator (^[c p] (write-char c p) p) |
| 92 |
(open-output-string) |
| 93 |
get-output-string)) |
| 94 |
|
| 95 |
(define (bytevector-accumulator) |
| 96 |
(make-accumulator (^[b p] (write-byte b p) p) |
| 97 |
(open-output-string) |
| 98 |
(^p (string->utf8 (get-output-string p))))) |
| 99 |
|
| 100 |
(define (bytevector-accumulator! vec at) |
| 101 |
(assume-type vec <u8vector>) |
| 102 |
(make-accumulator (^[v i] |
| 103 |
(when (>= i (uvector-length vec)) |
| 104 |
(error "bytevector is full")) |
| 105 |
(u8vector-set! vec i v) |
| 106 |
(+ i 1)) |
| 107 |
at |
| 108 |
(^_ vec))) |
| 109 |
|
| 110 |
(define (sum-accumulator) |
| 111 |
(make-accumulator + 0 identity)) |
| 112 |
(define (product-accumulator) |
| 113 |
(make-accumulator * 1 identity)) |
| 114 |
(define (count-accumulator) |
| 115 |
(make-accumulator (^[_ c] (+ c 1)) 0 identity)) |