2004-08-15 19:23:47 -04:00
|
|
|
! Copyright (C) 2004 Chris Double.
|
2006-09-12 21:32:23 -04:00
|
|
|
! See http://factorcode.org/license.txt for BSD license.
|
|
|
|
!
|
|
|
|
! Updated by Matthew Willis, July 2006
|
|
|
|
|
|
|
|
USING: kernel sequences math vectors arrays namespaces generic ;
|
2006-07-22 17:50:36 -04:00
|
|
|
IN: lazy-lists
|
2004-08-15 19:23:47 -04:00
|
|
|
|
2006-09-12 21:32:23 -04:00
|
|
|
TUPLE: cons car cdr ;
|
2006-09-12 22:17:23 -04:00
|
|
|
GENERIC: car ( cons -- car )
|
|
|
|
GENERIC: cdr ( cons -- cdr )
|
|
|
|
GENERIC: nil? ( cons -- bool )
|
2006-09-12 21:32:23 -04:00
|
|
|
|
|
|
|
C: cons ( car cdr -- list )
|
|
|
|
[ set-cons-cdr ] keep
|
|
|
|
[ set-cons-car ] keep ;
|
|
|
|
|
|
|
|
M: cons car ( cons -- car )
|
|
|
|
cons-car ;
|
|
|
|
|
2006-09-12 22:17:23 -04:00
|
|
|
: nil ( -- cons )
|
|
|
|
T{ cons f f f } ;
|
2006-09-12 21:32:23 -04:00
|
|
|
|
2006-09-12 22:17:23 -04:00
|
|
|
M: cons nil? ( cons -- bool )
|
|
|
|
nil eq? ;
|
2006-09-12 21:32:23 -04:00
|
|
|
|
|
|
|
: cons ( car cdr -- list )
|
|
|
|
<cons> ;
|
|
|
|
|
|
|
|
: 1list ( obj -- cons )
|
|
|
|
nil <cons> ;
|
|
|
|
|
|
|
|
: 2list ( obj obj -- cons )
|
|
|
|
nil <cons> <cons> ;
|
|
|
|
|
2005-04-29 23:20:11 -04:00
|
|
|
TUPLE: promise quot forced? value ;
|
2004-08-15 19:23:47 -04:00
|
|
|
|
2006-07-22 16:48:42 -04:00
|
|
|
C: promise ( quot -- promise ) [ set-promise-quot ] keep ;
|
2006-07-22 06:52:22 -04:00
|
|
|
|
2006-07-22 16:48:42 -04:00
|
|
|
: force ( promise -- value )
|
2006-07-22 06:52:22 -04:00
|
|
|
#! Force the given promise leaving the value of calling the
|
|
|
|
#! promises quotation on the stack. Re-forcing the promise
|
|
|
|
#! will return the same value and not recall the quotation.
|
|
|
|
dup promise-forced? [
|
|
|
|
dup promise-quot call over set-promise-value
|
|
|
|
t over set-promise-forced?
|
|
|
|
] unless
|
|
|
|
promise-value ;
|
2006-09-12 08:53:07 -04:00
|
|
|
|
|
|
|
! Both 'car' and 'cdr' are promises
|
2006-09-12 21:32:23 -04:00
|
|
|
: lazy-cons ( car cdr -- promise )
|
|
|
|
>r <promise> r> <promise> <cons>
|
|
|
|
T{ promise f f t f } clone [ set-promise-value ] keep ;
|
2004-08-15 19:23:47 -04:00
|
|
|
|
2006-09-12 21:32:23 -04:00
|
|
|
M: promise car ( promise -- car )
|
|
|
|
force car force ;
|
2004-08-15 19:23:47 -04:00
|
|
|
|
2006-09-12 21:32:23 -04:00
|
|
|
M: promise cdr ( promise -- cdr )
|
|
|
|
force cdr force ;
|
2004-08-15 19:23:47 -04:00
|
|
|
|
2006-09-12 22:17:23 -04:00
|
|
|
M: promise nil? ( cons -- bool )
|
|
|
|
force nil? ;
|
|
|
|
|
2006-09-12 21:32:23 -04:00
|
|
|
DEFER: lunit
|
|
|
|
DEFER: lnth
|
|
|
|
TUPLE: list ;
|
2004-08-15 19:23:47 -04:00
|
|
|
|
2006-09-12 21:32:23 -04:00
|
|
|
: 1lazy-list ( a -- lazy-cons )
|
|
|
|
[ nil ] lazy-cons ;
|
2004-08-15 19:23:47 -04:00
|
|
|
|
2006-09-12 21:32:23 -04:00
|
|
|
: 2lazy-list ( a b -- lazy-cons )
|
|
|
|
1lazy-list unit lazy-cons ;
|
2005-04-29 23:20:11 -04:00
|
|
|
|
2006-09-12 21:32:23 -04:00
|
|
|
: 3lazy-list ( a b c -- lazy-cons )
|
|
|
|
2lazy-list unit lazy-cons ;
|
2005-04-29 23:20:11 -04:00
|
|
|
|
2006-09-12 21:32:23 -04:00
|
|
|
: lnth ( n list -- elt )
|
|
|
|
swap [ cdr ] times car ;
|
2004-08-15 19:23:47 -04:00
|
|
|
|
2006-07-22 06:52:22 -04:00
|
|
|
: uncons ( cons -- car cdr )
|
|
|
|
#! Return the car and cdr of the lazy list
|
2006-09-12 08:53:07 -04:00
|
|
|
dup car swap cdr ;
|
2006-07-22 16:48:42 -04:00
|
|
|
|
2006-09-12 08:53:07 -04:00
|
|
|
: 2curry ( a b quot -- quot )
|
|
|
|
curry curry ;
|
2006-07-21 19:39:35 -04:00
|
|
|
|
2006-09-12 21:32:23 -04:00
|
|
|
TUPLE: lazy-map cons quot ;
|
|
|
|
|
2006-07-22 06:52:22 -04:00
|
|
|
: lmap ( list quot -- list )
|
2006-07-22 16:48:42 -04:00
|
|
|
#! Return a lazy list containing the collected result of calling
|
|
|
|
#! quot on the original lazy list.
|
2006-09-12 22:17:23 -04:00
|
|
|
over nil? [ 2drop nil ] [ <lazy-map> ] if ;
|
2006-09-12 21:32:23 -04:00
|
|
|
|
|
|
|
M: lazy-map car ( lazy-map -- car )
|
|
|
|
[ lazy-map-cons car ] keep
|
|
|
|
lazy-map-quot call ;
|
|
|
|
|
|
|
|
M: lazy-map cdr ( lazy-map -- cdr )
|
|
|
|
[ lazy-map-cons cdr ] keep
|
|
|
|
lazy-map-quot lmap ;
|
|
|
|
|
|
|
|
TUPLE: lazy-take n cons ;
|
2004-08-15 19:23:47 -04:00
|
|
|
|
2006-07-22 06:52:22 -04:00
|
|
|
: ltake ( n list -- list )
|
2006-07-22 16:48:42 -04:00
|
|
|
#! Return a lazy list containing the first n items from
|
|
|
|
#! the original lazy list.
|
2006-09-12 21:32:23 -04:00
|
|
|
over zero? [ 2drop nil ] [ <lazy-take> ] if ;
|
|
|
|
|
|
|
|
M: lazy-take car ( lazy-take -- car )
|
|
|
|
lazy-take-cons car ;
|
|
|
|
|
|
|
|
M: lazy-take cdr ( lazy-take -- cdr )
|
|
|
|
[ lazy-take-n 1- ] keep
|
|
|
|
lazy-take-cons cdr ltake ;
|
2005-04-29 23:20:11 -04:00
|
|
|
|
2006-09-12 22:17:23 -04:00
|
|
|
TUPLE: lazy-subset cons quot ;
|
|
|
|
|
|
|
|
: lsubset ( list quot -- list )
|
|
|
|
over nil? [ 2drop nil ] [ <lazy-subset> ] if ;
|
|
|
|
|
|
|
|
: car-subset? ( lazy-subset -- )
|
|
|
|
[ lazy-subset-cons car ] keep
|
|
|
|
lazy-subset-quot call ;
|
|
|
|
|
|
|
|
: skip ( lazy-subset -- )
|
|
|
|
[ lazy-subset-cons cdr ] keep
|
|
|
|
set-lazy-subset-cons ;
|
|
|
|
|
|
|
|
M: lazy-subset car ( lazy-subset -- car )
|
|
|
|
dup car-subset? [
|
|
|
|
lazy-subset-cons car
|
|
|
|
] [
|
|
|
|
dup skip car
|
|
|
|
] if ;
|
|
|
|
|
|
|
|
M: lazy-subset cdr ( lazy-subset -- cdr )
|
|
|
|
dup car-subset? [
|
|
|
|
[ lazy-subset-cons cdr ] keep
|
|
|
|
lazy-subset-quot lsubset
|
2006-09-12 08:53:07 -04:00
|
|
|
] [
|
2006-09-12 22:17:23 -04:00
|
|
|
dup skip cdr
|
2006-09-12 08:53:07 -04:00
|
|
|
] if ;
|
|
|
|
|
2006-09-12 22:17:23 -04:00
|
|
|
M: lazy-subset nil? ( lazy-subset -- bool )
|
|
|
|
dup lazy-subset-cons nil? [
|
|
|
|
drop t
|
|
|
|
] [
|
|
|
|
dup car-subset? [
|
|
|
|
drop f
|
|
|
|
] [
|
|
|
|
dup skip nil?
|
|
|
|
] if
|
|
|
|
] if ;
|
2006-09-12 08:53:07 -04:00
|
|
|
|
|
|
|
: t1
|
|
|
|
[ 1 ] [ [ 2 ] [ [ 3 ] [ nil ] cons ] cons ] cons ;
|
|
|
|
|
|
|
|
: t2
|
|
|
|
[ 2 ] [ [ 3 ] [ [ 4 ] [ nil ] cons ] cons ] cons ;
|
2006-07-22 06:52:22 -04:00
|
|
|
|
|
|
|
: (list>backwards-vector) ( list -- vector )
|
|
|
|
dup nil? [ drop V{ } clone ]
|
|
|
|
[ uncons (list>backwards-vector) swap over push ] if ;
|
|
|
|
|
|
|
|
: list>vector ( list -- vector )
|
|
|
|
#! Convert a lazy list to a vector. This will cause
|
|
|
|
#! an infinite loop if the lazy list is an infinite list.
|
|
|
|
(list>backwards-vector) reverse ;
|
|
|
|
|
|
|
|
: list>array ( list -- array )
|
|
|
|
list>vector >array ;
|
|
|
|
|
|
|
|
DEFER: backwards-vector>list
|
|
|
|
: (backwards-vector>list) ( vector -- list )
|
|
|
|
dup empty? [ drop nil ]
|
|
|
|
[ dup pop swap backwards-vector>list cons ] if ;
|
|
|
|
|
2006-09-12 21:32:23 -04:00
|
|
|
DEFER: force-promise
|
|
|
|
|
2006-07-22 06:52:22 -04:00
|
|
|
: backwards-vector>list ( vector -- list )
|
2006-07-22 16:48:42 -04:00
|
|
|
[ , \ (backwards-vector>list) , ] force-promise ;
|
2006-07-22 06:52:22 -04:00
|
|
|
|
|
|
|
: array>list ( array -- list )
|
|
|
|
#! Convert a list to a lazy list.
|
|
|
|
reverse >vector backwards-vector>list ;
|
2004-08-15 19:23:47 -04:00
|
|
|
|
2006-07-21 19:39:35 -04:00
|
|
|
DEFER: lappend*
|
2006-07-22 06:52:22 -04:00
|
|
|
: (lappend*) ( lists -- list )
|
|
|
|
dup nil? [
|
|
|
|
uncons >r dup nil? [ drop r> (lappend*) ]
|
|
|
|
[ uncons r> cons lappend* cons ] if
|
2006-07-21 19:39:35 -04:00
|
|
|
] unless ;
|
|
|
|
|
|
|
|
: lappend* ( llists -- list )
|
2006-07-22 06:52:22 -04:00
|
|
|
#! Given a lazy list of lazy lists, concatenate them
|
|
|
|
#! together in a lazy fashion. The actual appending is
|
|
|
|
#! done lazily on iteration rather than immediately
|
|
|
|
#! so it works very fast no matter how large the lists.
|
2006-07-22 16:48:42 -04:00
|
|
|
[ , \ (lappend*) , ] force-promise ;
|
2006-07-22 06:52:22 -04:00
|
|
|
|
|
|
|
: lappend ( list1 list2 -- llist )
|
|
|
|
#! Concatenate two lazy lists such that they appear to be one big
|
|
|
|
#! lazy list.
|
|
|
|
lunit cons lappend* ;
|
|
|
|
|
|
|
|
: leach ( list quot -- )
|
|
|
|
#! Call the quotation on each item in the lazy list.
|
|
|
|
#! Warning: If the list is infinite then this will
|
|
|
|
#! never return.
|
|
|
|
swap dup nil? [ 2drop ] [
|
|
|
|
uncons swap pick call swap leach
|
|
|
|
] if ;
|
|
|
|
|
|
|
|
DEFER: lapply
|
|
|
|
: (lapply) ( list quot -- list )
|
|
|
|
over nil? [ drop ] [
|
|
|
|
swap dup car >r uncons pick call swap lapply
|
|
|
|
r> swap cons
|
2006-07-21 19:39:35 -04:00
|
|
|
] if ;
|
|
|
|
|
2006-07-22 06:52:22 -04:00
|
|
|
: lapply ( list quot -- list )
|
|
|
|
#! Returns a lazy list which is
|
|
|
|
#! (cons (car list)
|
|
|
|
#! (lapply (quot (car list) (cdr list)) quot))
|
2006-07-21 19:39:35 -04:00
|
|
|
#! This allows for complicated list functions
|
2006-07-22 16:48:42 -04:00
|
|
|
[ swap , , \ (lapply) , ] force-promise ;
|
2006-07-21 19:39:35 -04:00
|
|
|
|
2006-07-22 06:52:22 -04:00
|
|
|
DEFER: lfrom-by
|
|
|
|
: (lfrom-by) ( n quot -- list )
|
|
|
|
2dup call swap lfrom-by cons ;
|
|
|
|
|
|
|
|
: lfrom-by ( n quot -- list )
|
|
|
|
#! Return a lazy list of values starting from n, with
|
|
|
|
#! each successive value being the result of applying quot to
|
|
|
|
#! n.
|
2006-07-22 16:48:42 -04:00
|
|
|
[ swap , , \ (lfrom-by) , ] force-promise ;
|
|
|
|
|
|
|
|
: lfrom ( n -- list )
|
|
|
|
#! Return a lazy list of increasing numbers starting
|
|
|
|
#! from the initial value 'n'.
|
|
|
|
[ 1 + ] lfrom-by ;
|