Debian Codegraph @

src/liblazy.scm

raw
1 ;;;
2 ;;;liblazy.scm - lazy constructs
3 ;;;
4 ;;; Copyright (c) 2000-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 (select-module gauche.internal)
35
36 (declare (keep-private-macro lcons lcons* llist*))
37
38 (inline-stub
39 (.include "gauche/priv/configP.h"
40 "gauche/priv/promiseP.h"))
41
42 ;;;
43 ;;; delay/force/lazy/eager
44 ;;;
45
46 ;; NB: delay and lazy is recognized by the compiler and directly
47 ;; expanded into PROMISE instruction.
48
49 (select-module scheme)
50 (define-cproc force (p) Scm_VMForce)
51
52 (select-module gauche)
53 (define-cproc promise? (obj) ::<boolean> :constant
54 (return (SCM_XTYPEP obj SCM_CLASS_PROMISE)))
55 (define-cproc eager (:rest objs) ;SRFI-45
56 (return (Scm_MakePromise SCM_PROMISE_FORCED objs)))
57 (define-cproc promise-kind (p::<promise>)
58 (setter (p::<promise> obj) ::<void> (set! (-> p kind) obj))
59 (return (-> p kind)))
60 (define make-promise eager) ;SRFI-226
61
62 ;;;
63 ;;; lazy sequence
64 ;;;
65
66 (select-module gauche.internal)
67
68 ;; A primitive for corecursion.
69 ;; See libmacro.scm for lcons macro.
70 (define-cproc %lcons (item thunk :optional (attrs ()))
71 (return (Scm_LazyCons item thunk attrs)))
72
73 ;; lazy sequence primitives
74 ;; These are so fundamental that they deserve to be in core.
75 ;; Auxiliary utilities are provided in gauche.lazy module.
76
77 (select-module gauche)
78
79 ;; Fundamental constructor
80 ;; generator->lseq generator
81 ;; generator->lseq item ... generator
82 (define-cproc generator->lseq (item :rest args)
83 (if (SCM_NULLP args)
84 (return (Scm_GeneratorToLazyPair item)) ;item is a generator
85 (let* ([h SCM_NIL] [t SCM_NIL])
86 (for ()
87 (when (SCM_NULLP (SCM_CDR args))
88 (if (SCM_NULLP t)
89 (return (Scm_MakeLazyPair item (SCM_CAR args) SCM_NIL))
90 (begin
91 (SCM_SET_CDR t (Scm_MakeLazyPair item (SCM_CAR args) SCM_NIL))
92 (return h))))
93 (SCM_APPEND1 h t item)
94 (set! item (SCM_CAR args))
95 (set! args (SCM_CDR args))))))
96
97 ;; For convenience.
98 (define (lrange start :optional (end +inf.0) (step 1))
99 (cond [(or (and (> step 0) (>= start end))
100 (and (< step 0) (<= start end))) '()]
101 [(= step 0) (generator->lseq (^[] start))]
102 [(and (exact? start) (exact? step))
103 (generator->lseq start
104 (if (> step 0)
105 (^[] (inc! start step)
106 (if (< start end) start (eof-object)))
107 (^[] (inc! start step)
108 (if (> start end) start (eof-object)))))]
109 [else
110 (generator->lseq (inexact start)
111 (let1 c 0
112 (if (> step 0)
113 (^[] (inc! c)
114 (let1 r (+ start (* c step))
115 (if (< r end) r (eof-object))))
116 (^[] (inc! c)
117 (let1 r (+ start (* c step))
118 (if (> r end) r (eof-object))))
119 )))]))
120
121 (define (liota :optional (count +inf.0) (start 0) (step 1))
122 (let1 count (if (< count 0) +inf.0 count) ; like stream-iota
123 (define gen
124 (if (and (exact? start) (exact? step))
125 (if (infinite? count)
126 (^[] (rlet1 v start (inc! start step)))
127 (^[] (if (<= count 0)
128 (eof-object)
129 (rlet1 v start (inc! start step) (dec! count)))))
130 (let1 k 0
131 (if (infinite? count)
132 (^[] (rlet1 v (+ start (* k step)) (inc! k)))
133 (^[] (if (<= count 0)
134 (eof-object)
135 (rlet1 v (+ start (* k step)) (inc! k) (dec! count))))))))
136 (generator->lseq gen)))
137
138 (define (port->char-lseq :optional (port (current-input-port)))
139 (generator->lseq (cut read-char port)))
140 (define (port->byte-lseq :optional (port (current-input-port)))
141 (generator->lseq (cut read-byte port)))
142 (define (port->string-lseq :optional (port (current-input-port)))
143 (generator->lseq (cut read-line port)))
144 (define (port->sexp-lseq :optional (port (current-input-port)))
145 (generator->lseq (cut read port)))