1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
|
;; -*-theme-d-*-
;; Copyright (C) 2016, 2021 Tommi Höynälänmaa
;; Distributed under GNU Lesser General Public License version 3,
;; see file doc/LGPL-3.
(define-body (standard-library stream)
(define-param-method stream-next (%type)
(((stream (:stream %type)))
(:stream %type)
pure)
(match-type stream
((<null>) (raise-simple 'stream-next:empty-stream))
((stream2 (:nonempty-stream %type))
(force (cdr stream2)))))
(define-param-method stream-value (%type)
(((stream (:stream %type)))
%type
pure)
(match-type stream
((<null>) (raise-simple 'stream-value:empty-stream))
((stream2 (:nonempty-stream %type))
(force (car stream2)))))
(define-param-method stream-empty? (%type)
(((stream (:stream %type)))
<boolean>
pure)
(null? stream))
(define-param-method stream->list (%type)
(((stream (:stream %type)))
(:uniform-list %type)
pure)
(match-type stream
((<null>) null)
((stream2 (:nonempty-stream %type))
(cons (force (car stream2))
(stream->list (stream-next stream2))))))
(define-param-method list->stream (%type)
(((l (:uniform-list %type)))
(:stream %type)
pure)
(match-type l
((<null>) null)
((l2 (:nonempty-uniform-list %type))
(cons (delay (car l2)) (delay (list->stream (cdr l2)))))))
(define-param-method nonpure-stream-next (%type)
(((stream (:nonpure-stream %type)))
(:nonpure-stream %type)
nonpure)
(match-type stream
((<null>) (raise-simple 'nonpure-stream-next:empty-stream))
((stream2 (:nonempty-nonpure-stream %type))
(force-nonpure (cdr stream2)))))
(define-param-method nonpure-stream-value (%type)
(((stream (:nonpure-stream %type)))
%type
nonpure)
(match-type stream
((<null>) (raise-simple 'nonpure-stream-value:empty-stream))
((stream2 (:nonempty-nonpure-stream %type))
(force-nonpure (car stream2)))))
(define-param-method nonpure-stream-empty? (%type)
(((stream (:nonpure-stream %type)))
<boolean>
pure)
(null? stream))
(define-param-method nonpure-stream->list (%type)
(((stream (:nonpure-stream %type)))
(:uniform-list %type)
nonpure)
(match-type stream
((<null>) null)
((stream2 (:nonempty-nonpure-stream %type))
(cons (force-nonpure (car stream2))
(nonpure-stream->list
(nonpure-stream-next stream2))))))
(define-param-method stream-map (%type1 %type2)
(((proc (:procedure (%type1) %type2 pure))
(stm (:stream %type1)))
(:stream %type2)
pure)
(match-type stm
((<null>) null)
((stm2 (:nonempty-stream %type1))
(cons (delay (proc (force (car stm2))))
(delay (stream-map proc (stream-next stm2)))))))
(define-param-method stream-map-nonpure (%type1 %type2)
(((proc (:procedure (%type1) %type2 nonpure))
(stm (:stream %type1)))
(:nonpure-stream %type2)
nonpure)
(match-type stm
((<null>) null)
((stm2 (:nonempty-stream %type1))
(cons (delay-nonpure (proc (force (car stm2))))
(delay-nonpure
(stream-map-nonpure proc (stream-next stm2)))))))
(define-param-method stream-for-each (%type1)
(((proc (:procedure (%type1) <none> nonpure))
(stm (:stream %type1)))
<none>
nonpure)
(match-type stm
((<null>) null)
((stm2 (:nonempty-stream %type1))
(proc (stream-value stm2))
(stream-for-each proc (stream-next stm2)))))
(define-param-method nonpure-stream-map (%type1 %type2)
(((proc (:procedure (%type1) %type2 nonpure))
(stm (:nonpure-stream %type1)))
(:nonpure-stream %type2)
nonpure)
(match-type stm
((<null>) null)
((stm2 (:nonempty-nonpure-stream %type1))
(cons (delay-nonpure (proc (force-nonpure (car stm2))))
(delay-nonpure
(nonpure-stream-map proc (nonpure-stream-next stm2)))))))
(define-param-method nonpure-stream-for-each (%type1)
(((proc (:procedure (%type1) <none> nonpure))
(stm (:nonpure-stream %type1)))
<none>
nonpure)
(match-type stm
((<null>) null)
((stm2 (:nonempty-nonpure-stream %type1))
(proc (force-nonpure (car stm2)))
(nonpure-stream-for-each proc (nonpure-stream-next stm2)))))
(define-simple-method make-input-expr-stream
(((ip <input-port>)) (:nonpure-stream <object>) nonpure)
(if (eof? (peek-character ip))
null
(cons (delay-nonpure (read ip))
(delay-nonpure (make-input-expr-stream ip))))))
|