1 ! Copyright (C) 2004 Chris Double.
2 ! See http://factorcode.org/license.txt for BSD license.
4 ! Updated by Matthew Willis, July 2006
5 ! Updated by Chris Double, September 2006
6 ! Updated by James Cash, June 2008
8 USING: kernel sequences math vectors arrays namespaces make
9 quotations promises combinators io lists accessors ;
12 M: promise car ( promise -- car )
15 M: promise cdr ( promise -- cdr )
18 M: promise nil? ( cons -- bool )
21 ! Both 'car' and 'cdr' are promises
22 TUPLE: lazy-cons car cdr ;
24 : lazy-cons ( car cdr -- promise )
25 [ promise ] bi@ \ lazy-cons boa
26 T{ promise f f t f } clone
29 M: lazy-cons car ( lazy-cons -- car )
32 M: lazy-cons cdr ( lazy-cons -- cdr )
35 M: lazy-cons nil? ( lazy-cons -- bool )
38 : 1lazy-list ( a -- lazy-cons )
41 : 2lazy-list ( a b -- lazy-cons )
42 1lazy-list 1quotation lazy-cons ;
44 : 3lazy-list ( a b c -- lazy-cons )
45 2lazy-list 1quotation lazy-cons ;
47 TUPLE: memoized-cons original car cdr nil? ;
49 : not-memoized ( -- obj )
52 : not-memoized? ( obj -- bool )
55 : <memoized-cons> ( cons -- memoized-cons )
56 not-memoized not-memoized not-memoized
59 M: memoized-cons car ( memoized-cons -- car )
60 dup car>> not-memoized? [
61 dup original>> car [ >>car drop ] keep
66 M: memoized-cons cdr ( memoized-cons -- cdr )
67 dup cdr>> not-memoized? [
68 dup original>> cdr [ >>cdr drop ] keep
73 M: memoized-cons nil? ( memoized-cons -- bool )
74 dup nil?>> not-memoized? [
75 dup original>> nil? [ >>nil? drop ] keep
80 TUPLE: lazy-map cons quot ;
82 C: <lazy-map> lazy-map
84 : lazy-map ( list quot -- result )
85 over nil? [ 2drop nil ] [ <lazy-map> <memoized-cons> ] if ;
87 M: lazy-map car ( lazy-map -- car )
91 M: lazy-map cdr ( lazy-map -- cdr )
95 M: lazy-map nil? ( lazy-map -- bool )
98 : lazy-map-with ( value list quot -- result )
101 TUPLE: lazy-take n cons ;
103 C: <lazy-take> lazy-take
105 : ltake ( n list -- result )
106 over zero? [ 2drop nil ] [ <lazy-take> ] if ;
108 M: lazy-take car ( lazy-take -- car )
111 M: lazy-take cdr ( lazy-take -- cdr )
115 M: lazy-take nil? ( lazy-take -- bool )
122 TUPLE: lazy-until cons quot ;
124 C: <lazy-until> lazy-until
126 : luntil ( list quot -- result )
127 over nil? [ drop ] [ <lazy-until> ] if ;
129 M: lazy-until car ( lazy-until -- car )
132 M: lazy-until cdr ( lazy-until -- cdr )
133 [ cons>> uncons ] keep quot>> tuck call
134 [ 2drop nil ] [ luntil ] if ;
136 M: lazy-until nil? ( lazy-until -- bool )
139 TUPLE: lazy-while cons quot ;
141 C: <lazy-while> lazy-while
143 : lwhile ( list quot -- result )
144 over nil? [ drop ] [ <lazy-while> ] if ;
146 M: lazy-while car ( lazy-while -- car )
149 M: lazy-while cdr ( lazy-while -- cdr )
150 [ cons>> cdr ] keep quot>> lwhile ;
152 M: lazy-while nil? ( lazy-while -- bool )
153 [ car ] keep quot>> call not ;
155 TUPLE: lazy-filter cons quot ;
157 C: <lazy-filter> lazy-filter
159 : lfilter ( list quot -- result )
160 over nil? [ 2drop nil ] [ <lazy-filter> <memoized-cons> ] if ;
162 : car-filter? ( lazy-filter -- ? )
163 [ cons>> car ] [ quot>> ] bi call ;
165 : skip ( lazy-filter -- )
166 dup cons>> cdr >>cons drop ;
168 M: lazy-filter car ( lazy-filter -- car )
169 dup car-filter? [ cons>> ] [ dup skip ] if car ;
171 M: lazy-filter cdr ( lazy-filter -- cdr )
173 [ cons>> cdr ] [ quot>> ] bi lfilter
178 M: lazy-filter nil? ( lazy-filter -- bool )
189 : list>vector ( list -- vector )
190 [ [ , ] leach ] V{ } make ;
192 : list>array ( list -- array )
193 [ [ , ] leach ] { } make ;
195 TUPLE: lazy-append list1 list2 ;
197 C: <lazy-append> lazy-append
199 : lappend ( list1 list2 -- result )
200 over nil? [ nip ] [ <lazy-append> ] if ;
202 M: lazy-append car ( lazy-append -- car )
205 M: lazy-append cdr ( lazy-append -- cdr )
209 M: lazy-append nil? ( lazy-append -- bool )
212 TUPLE: lazy-from-by n quot ;
214 C: lfrom-by lazy-from-by ( n quot -- list )
216 : lfrom ( n -- list )
219 M: lazy-from-by car ( lazy-from-by -- car )
222 M: lazy-from-by cdr ( lazy-from-by -- cdr )
224 quot>> dup slip lfrom-by ;
226 M: lazy-from-by nil? ( lazy-from-by -- bool )
229 TUPLE: lazy-zip list1 list2 ;
231 C: <lazy-zip> lazy-zip
233 : lzip ( list1 list2 -- lazy-zip )
234 over nil? over nil? or
235 [ 2drop nil ] [ <lazy-zip> ] if ;
237 M: lazy-zip car ( lazy-zip -- car )
238 [ list1>> car ] keep list2>> car 2array ;
240 M: lazy-zip cdr ( lazy-zip -- cdr )
241 [ list1>> cdr ] keep list2>> cdr lzip ;
243 M: lazy-zip nil? ( lazy-zip -- bool )
246 TUPLE: sequence-cons index seq ;
248 C: <sequence-cons> sequence-cons
250 : seq>list ( index seq -- list )
257 M: sequence-cons car ( sequence-cons -- car )
261 M: sequence-cons cdr ( sequence-cons -- cdr )
265 M: sequence-cons nil? ( sequence-cons -- bool )
268 : >list ( object -- list )
270 { [ dup sequence? ] [ 0 swap seq>list ] }
271 { [ dup list? ] [ ] }
272 [ "Could not convert object to a list" throw ]
275 TUPLE: lazy-concat car cdr ;
277 C: <lazy-concat> lazy-concat
281 : (lconcat) ( car cdr -- list )
288 : lconcat ( list -- result )
292 uncons swap (lconcat)
295 M: lazy-concat car ( lazy-concat -- car )
298 M: lazy-concat cdr ( lazy-concat -- cdr )
299 [ car>> cdr ] keep cdr>> (lconcat) ;
301 M: lazy-concat nil? ( lazy-concat -- bool )
308 : lcartesian-product ( list1 list2 -- result )
309 swap [ swap [ 2array ] lazy-map-with ] lazy-map-with lconcat ;
311 : lcartesian-product* ( lists -- result )
315 [ car ] keep cdr [ car lcartesian-product ] keep cdr list>array swap [
316 swap [ swap [ suffix ] lazy-map-with ] lazy-map-with lconcat
320 : lcomp ( list quot -- result )
321 [ lcartesian-product* ] dip lazy-map ;
323 : lcomp* ( list guards quot -- result )
324 [ [ lcartesian-product* ] dip [ lfilter ] each ] dip lazy-map ;
328 : (lmerge) ( list1 list2 -- result )
329 over [ car ] curry -rot
331 dup [ car ] curry -rot
337 : lmerge ( list1 list2 -- result )
339 { [ over nil? ] [ nip ] }
340 { [ dup nil? ] [ drop ] }
341 { [ t ] [ (lmerge) ] }
344 TUPLE: lazy-io stream car cdr quot ;
348 : lcontents ( stream -- result )
349 f f [ stream-read1 ] <lazy-io> ;
351 : llines ( stream -- result )
352 f f [ stream-readln ] <lazy-io> ;
354 M: lazy-io car ( lazy-io -- car )
358 drop dup stream>> over quot>> call
362 M: lazy-io cdr ( lazy-io -- cdr )
370 [ f f ] dip <lazy-io> [ >>cdr drop ] keep
376 M: lazy-io nil? ( lazy-io -- bool )
379 INSTANCE: sequence-cons list
380 INSTANCE: memoized-cons list
381 INSTANCE: promise list
382 INSTANCE: lazy-io list
383 INSTANCE: lazy-concat list
384 INSTANCE: lazy-cons list
385 INSTANCE: lazy-map list
386 INSTANCE: lazy-take list
387 INSTANCE: lazy-append list
388 INSTANCE: lazy-from-by list
389 INSTANCE: lazy-zip list
390 INSTANCE: lazy-while list
391 INSTANCE: lazy-until list
392 INSTANCE: lazy-filter list