Debian Codegraph @

%3a127/lseqs-impl.scm

raw symbols SCIP
1 ;; Helper returns #t if any element of list is null or #f if none
2 (define (any-null? list)
3 (cond
4 ((null? list) #f)
5 ((null? (car list)) #t)
6 (else (any-null? (cdr list)))))
7
8 ;; gappend procedure cloned from SRFI 121
9 (define (gappend . args)
10 (lambda () (if (null? args)
11 (eof-object)
12 (let loop ((v ((car args))))
13 (if (eof-object? v)
14 (begin (set! args (cdr args))
15 (if (null? args)
16 (eof-object)
17 (loop ((car args)))))
18 v)))))
19
20 ;;; Convert a generator (procedure with no arguments) to an lseq
21 ;;; This is the basic constructor for lseqs, since every proper list
22 ;;; is already an lseq and so list->lseq is not needed
23 (define (generator->lseq gen)
24 (let ((value (gen)))
25 ;; See what starts off the generator:
26 ;; if it's already exhausted, the lseq is empty,
27 ;; otherwise, return an improper list with one value and the generator
28 ;; in the tail, which is how we represent unrealized lseqs
29 (if (eof-object? value)
30 '()
31 (cons value gen))))
32
33 ;;; Car on lseqs is the same as on lists
34 (define (lseq-car lseq) (car lseq))
35 (define (lseq-first lseq) (car lseq))
36
37 ;;; Lseq-cdr expands the generator if it's there, or falls back to regular cdr
38 (define (lseq-cdr lseq)
39 ;; We assume lseq is a pair, because it is an error if it isn't
40 ;; If it's a procedure, we assume it's a generator and invoke it
41 (if (procedure? (cdr lseq))
42 (let ((obj ((cdr lseq))))
43 (cond
44 ;; If the generator is exhausted, replace it with () and return ()
45 ((eof-object? obj)
46 (set-cdr! lseq '())
47 '())
48 ;; Otherwise, make a new pair of the value and the generator
49 ;; and patch it in to the cdr
50 (else (let ((result (cons obj (cdr lseq))))
51 (set-cdr! lseq result)
52 result))))
53 ;; If there is no procedure, return the ordinary cdr
54 (cdr lseq)))
55
56 (define (lseq-rest lseq) (lseq-cdr lseq))
57
58 ;;; Returns #t if argument is an lseq
59 ;;; Note that without arity inspection, we can't be sure a procedure in the
60 ;;; tail is really a generator, so we assume it is
61 (define (lseq? obj)
62 (cond
63 ;; null list is a lseq
64 ((null? obj) #t)
65 ;; non-list is not an lseq
66 ((not (pair? obj)) #f)
67 ;; improper list with procedure in the tail is (presumed to be) an lseq
68 ((procedure? (cdr obj)) #t)
69 ;; otherwise keep looking
70 (else (lseq? (cdr obj)))))
71
72 ;;; Compare lseqs for equality
73 (define (lseq=? = lseq1 lseq2)
74 (cond
75 ((and (null? lseq1) (null? lseq2))
76 #t)
77 ((or (null? lseq1) (null? lseq2))
78 #f)
79 ((= (lseq-car lseq1) (lseq-car lseq2))
80 (lseq=? = (lseq-cdr lseq1) (lseq-cdr lseq2)))
81 (else #f)))
82
83 ;;; Take the first n elements of lseq and return as a list
84 (define (lseq-take lseq i)
85 (generator->lseq
86 (lambda ()
87 (if (= i 0)
88 (eof-object)
89 (let ((result (lseq-car lseq)))
90 (set! lseq (lseq-cdr lseq))
91 (set! i (- i 1))
92 result)))))
93
94 ;; Drop the first n arguments of lseq
95 ;; No reason not to do it eagerly
96 (define (lseq-drop lseq i)
97 (let loop ((i i) (lseq lseq))
98 (if (= i 0)
99 lseq
100 (loop (- i 1) (lseq-cdr lseq)))))
101
102 ;; Get the nth argument of lseq
103 (define (lseq-ref lseq i) (lseq-car (lseq-drop lseq i)))
104
105 ;;; Convert lseq to a list by lseq-cdr-ing down it to the end
106 (define (lseq-realize lseq)
107 (let loop ((next lseq))
108 (if (null? next)
109 lseq
110 (loop (lseq-cdr next)))))
111
112 ;;; Realize an lseq and return its length
113 (define (lseq-length lseq) (length (lseq-realize lseq)))
114
115 ;; Return a generator that steps through the elements of the lseq
116 (define (lseq->generator lseq)
117 (lambda ()
118 (if (null? lseq)
119 (eof-object)
120 (let ((result (lseq-car lseq)))
121 (set! lseq (lseq-cdr lseq))
122 result))))
123
124 ;; lseq-append converts lseqs to generators and gappends them
125
126 (define (lseq-append . lseqs)
127 (generator->lseq (apply gappend (map lseq->generator lseqs))))
128
129 ;; Safe version of lseq-cdr that returns () if the argument is ()
130 (define (safe-lseq-cdr obj)
131 (if (null? obj)
132 obj
133 (lseq-cdr obj)))
134
135 ;; Lazily map lseqs through a proc to produce another lseq
136 (define (lseq-map proc . lseqs)
137 (generator->lseq
138 (lambda ()
139 (if (any-null? lseqs)
140 (eof-object)
141 (let ((result (apply proc (map lseq-car lseqs))))
142 (set! lseqs (map safe-lseq-cdr lseqs))
143 result)))))
144
145 ;; Zip cars of lseqs into a list and return an lseq of those lists
146 (define (lseq-zip . lseqs) (apply lseq-map list lseqs))
147
148 ;; Eagerly apply a proc to the elements of lseqs
149 ;; Included because it's a common operation, even though it is trivial
150 (define (lseq-for-each proc . lseqs)
151 (apply for-each proc (map lseq-realize lseqs)))
152
153 ;; Filter an lseq lazily to include only elements that satisfy pred
154 (define (lseq-filter pred lseq)
155 (generator->lseq
156 (lambda ()
157 (let loop ((lseq1 lseq))
158 (if (null? lseq1)
159 (eof-object)
160 (let ((result (lseq-car lseq1)))
161 (cond
162 ((pred result)
163 (set! lseq (lseq-cdr lseq1))
164 result)
165 (else
166 (loop (lseq-cdr lseq1))))))))))
167
168
169 ;; Negated filter
170 (define (lseq-remove pred lseq)
171 (lseq-filter (lambda (x) (not (pred x))) lseq))
172
173 ;; Find an element that satisfies a pred, or #f if no such element
174 (define (lseq-find pred lseq)
175 (cond
176 ((null? lseq) #f)
177 ((pred (lseq-car lseq)) (lseq-car lseq))
178 (else (lseq-find pred (lseq-cdr lseq)))))
179
180 ;; Find the tail of an lseq whose car satisfies a pred, or #f if no such
181 (define (lseq-find-tail pred lseq)
182 (cond
183 ((null? lseq) #f)
184 ((pred (lseq-car lseq)) lseq)
185 (else (lseq-find-tail pred (lseq-cdr lseq)))))
186
187 ;; Return initial elements of lseq that satisfy pred
188 (define (lseq-take-while pred lseq)
189 (generator->lseq
190 (lambda ()
191 (if (not (pred (lseq-car lseq)))
192 (eof-object)
193 (let ((result (lseq-car lseq)))
194 (set! lseq (lseq-cdr lseq))
195 result)))))
196
197
198 ;; Return all but initial of lseq that satisfy pred
199 ;; No reason not to do it eagerly
200 (define (lseq-drop-while pred lseq)
201 (let loop ((lseq lseq))
202 (if (not (pred (lseq-car lseq)))
203 lseq
204 (loop (lseq-cdr lseq)))))
205
206 ;; Apply predicate across lseqs, returning result if it is true
207 (define (lseq-any pred . lseqs)
208 (let loop ((lseqs lseqs))
209 (if (any-null? lseqs)
210 #f
211 (let ((result (apply pred (map lseq-car lseqs))))
212 (if result
213 result
214 (loop (map lseq-cdr lseqs)))))))
215
216 ;; Apply predicate across lseqs, returning false if predicate does
217 (define (lseq-every pred . lseqs)
218 (let loop ((lseqs lseqs) (last-result #t))
219 (if (any-null? lseqs)
220 last-result
221 (let ((result (apply pred (map lseq-car lseqs))))
222 (if result
223 (loop (map lseq-cdr lseqs) result)
224 #f)))))
225
226 ;; Return the index of the first element of lseq that satisfies pred
227 (define (lseq-index pred . lseqs)
228 (let loop ((lseqs lseqs) (n 0))
229 (cond
230 ((any-null? lseqs) #f)
231 ((apply pred (map lseq-car lseqs)) n)
232 (else (loop (map safe-lseq-cdr lseqs) (+ n 1))))))
233
234 ;; Return tail of lseq whose first element is x in the sense of = (default equal?)
235 (define lseq-member
236 (case-lambda
237 ((x lseq) (lseq-member x lseq equal?))
238 ((x lseq =) (cond
239 ((null? lseq) #f)
240 ((= x (lseq-car lseq)) lseq)
241 (else (lseq-member x (lseq-cdr lseq) =))))))
242
243
244 ;; Member using eqv?
245 (define (lseq-memv x lseq) (lseq-member x lseq eqv?))
246
247 ;; Member using eq?
248 (define (lseq-memq x lseq) (lseq-member x lseq eq?))