1 ! Copyright (C) 2004, 2008 Chris Double, Matthew Willis, James Cash.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays combinators io kernel lists math
4 promises quotations sequences ;
7 M: promise car force car ;
9 M: promise cdr force cdr ;
11 M: promise nil? force nil? ;
13 TUPLE: lazy-cons-state
17 C: <lazy-cons-state> lazy-cons-state
19 : lazy-cons ( car cdr -- promise )
20 [ <promise> ] bi@ <lazy-cons-state>
21 [ f t ] dip promise boa ;
23 M: lazy-cons-state car car>> force ;
25 M: lazy-cons-state cdr cdr>> force ;
27 M: lazy-cons-state nil? nil eq? ;
29 : 1lazy-list ( a -- lazy-cons )
32 : 2lazy-list ( a b -- lazy-cons )
33 1lazy-list 1quotation lazy-cons ;
35 : 3lazy-list ( a b c -- lazy-cons )
36 2lazy-list 1quotation lazy-cons ;
38 TUPLE: memoized-cons original car cdr nil? ;
40 SYMBOL: +not-memoized+
42 : <memoized-cons> ( cons -- memoized-cons )
43 +not-memoized+ +not-memoized+ +not-memoized+
47 dup car>> +not-memoized+ eq? [
48 dup original>> car [ >>car drop ] keep
54 dup cdr>> +not-memoized+ eq? [
55 dup original>> cdr [ >>cdr drop ] keep
61 dup nil?>> +not-memoized+ eq? [
62 dup original>> nil? [ >>nil? drop ] keep
67 TUPLE: lazy-map cons quot ;
69 C: <lazy-map> lazy-map
71 : lmap-lazy ( list quot -- result )
72 over nil? [ 2drop nil ] [ <lazy-map> <memoized-cons> ] if ;
75 [ cons>> car ] [ quot>> call( old -- new ) ] bi ;
78 [ cons>> cdr ] [ quot>> lmap-lazy ] bi ;
83 TUPLE: lazy-take n cons ;
85 C: <lazy-take> lazy-take
87 : ltake ( n list -- result )
88 over zero? [ 2drop nil ] [ <lazy-take> ] if ;
94 [ n>> 1 - ] [ cons>> cdr ltake ] bi ;
97 dup n>> zero? [ drop t ] [ cons>> nil? ] if ;
99 TUPLE: lazy-until cons quot ;
101 C: <lazy-until> lazy-until
103 : luntil ( list quot -- result )
104 over nil? [ drop ] [ <lazy-until> ] if ;
110 [ [ cons>> cdr ] [ quot>> ] bi ]
111 [ [ cons>> car ] [ quot>> ] bi call( elt -- ? ) ] bi
112 [ 2drop nil ] [ luntil ] if ;
117 TUPLE: lazy-while cons quot ;
119 C: <lazy-while> lazy-while
121 : lwhile ( list quot -- result )
122 over nil? [ drop ] [ <lazy-while> ] if ;
128 [ cons>> cdr ] keep quot>> lwhile ;
131 [ car ] keep quot>> call( elt -- ? ) not ;
133 TUPLE: lazy-filter cons quot ;
135 C: <lazy-filter> lazy-filter
137 : lfilter ( list quot -- result )
138 over nil? [ 2drop nil ] [ <lazy-filter> <memoized-cons> ] if ;
142 : car-filter? ( lazy-filter -- ? )
143 [ cons>> car ] [ quot>> ] bi call( elt -- ? ) ;
145 : skip ( lazy-filter -- )
146 dup cons>> cdr >>cons drop ;
151 dup car-filter? [ cons>> ] [ dup skip ] if car ;
155 [ cons>> cdr ] [ quot>> ] bi lfilter
162 { [ dup cons>> nil? ] [ drop t ] }
163 { [ dup car-filter? ] [ drop f ] }
167 TUPLE: lazy-append list1 list2 ;
169 C: <lazy-append> lazy-append
171 : lappend-lazy ( list1 list2 -- result )
172 over nil? [ nip ] [ <lazy-append> ] if ;
178 [ list1>> cdr ] [ list2>> ] bi lappend-lazy ;
183 TUPLE: lazy-from-by n quot ;
185 : lfrom-by ( n quot: ( n -- o ) -- lazy-from-by ) lazy-from-by boa ; inline
187 : lfrom ( n -- list )
194 [ n>> ] [ quot>> ] bi [ call( old -- new ) ] keep lfrom-by ;
199 TUPLE: lazy-zip list1 list2 ;
201 C: <lazy-zip> lazy-zip
203 : lzip ( list1 list2 -- lazy-zip )
204 over nil? over nil? or
205 [ 2drop nil ] [ <lazy-zip> ] if ;
208 [ list1>> car ] keep list2>> car 2array ;
211 [ list1>> cdr ] keep list2>> cdr lzip ;
216 TUPLE: sequence-cons index seq ;
218 C: <sequence-cons> sequence-cons
220 : sequence-tail>list ( index seq -- list )
228 [ index>> ] [ seq>> nth ] bi ;
231 [ index>> 1 + ] [ seq>> sequence-tail>list ] bi ;
233 M: sequence-cons nil?
236 M: sequence >list 0 swap sequence-tail>list ;
238 TUPLE: lazy-concat car cdr ;
240 C: <lazy-concat> lazy-concat
244 : (lconcat) ( car cdr -- list )
245 over nil? [ nip lconcat ] [ <lazy-concat> ] if ;
247 : lconcat ( list -- result )
248 dup nil? [ drop nil ] [ uncons (lconcat) ] if ;
254 [ car>> cdr ] keep cdr>> (lconcat) ;
257 dup car>> nil? [ cdr>> nil? ] [ drop f ] if ;
259 : lcartesian-product ( list1 list2 -- result )
260 swap [ swap [ 2array ] with lmap-lazy ] with lmap-lazy lconcat ;
262 : lcartesian-product* ( lists -- result )
267 [ car lcartesian-product ] [ cdr ] bi
269 swap [ swap [ suffix ] with lmap-lazy ] with lmap-lazy lconcat
273 : lcomp ( list quot -- result )
274 [ lcartesian-product* ] dip lmap-lazy ;
276 : lcomp* ( list guards quot -- result )
277 [ [ lcartesian-product* ] dip [ lfilter ] each ] dip lmap-lazy ;
281 : (lmerge) ( list1 list2 -- result )
282 over [ car ] curry -rot
284 dup [ car ] curry -rot
290 : lmerge ( list1 list2 -- result )
292 { [ over nil? ] [ nip ] }
293 { [ dup nil? ] [ drop ] }
294 { [ t ] [ (lmerge) ] }
297 TUPLE: lazy-io stream car cdr quot ;
301 : lcontents ( stream -- result )
302 f f [ stream-read1 ] <lazy-io> ;
304 : llines ( stream -- result )
305 f f [ stream-readln ] <lazy-io> ;
311 [ ] [ stream>> ] [ quot>> ] tri
312 call( stream -- value ) [ >>car ] [ drop nil ] if*
319 drop dup [ stream>> ] [ quot>> ] [ car ] tri
321 [ f f ] dip <lazy-io> [ >>cdr drop ] keep
330 INSTANCE: sequence-cons list
331 INSTANCE: memoized-cons list
332 INSTANCE: promise list
333 INSTANCE: lazy-io list
334 INSTANCE: lazy-concat list
335 INSTANCE: lazy-cons-state list
336 INSTANCE: lazy-map list
337 INSTANCE: lazy-take list
338 INSTANCE: lazy-append list
339 INSTANCE: lazy-from-by list
340 INSTANCE: lazy-zip list
341 INSTANCE: lazy-while list
342 INSTANCE: lazy-until list
343 INSTANCE: lazy-filter list