Add lazy-while and lazy-until to lazy-lists
parent
858237c0f8
commit
d5cd2fd66b
|
@ -1,2 +1,3 @@
|
|||
Chris Double
|
||||
Samuel Tardieu
|
||||
Matthew Willis
|
||||
|
|
|
@ -114,6 +114,16 @@ HELP: lsubset
|
|||
{ $description "Perform a similar functionality to that of the " { $link subset } " word, but in a lazy manner. No evaluation of the list elements occurs initially but a " { $link <lazy-subset> } " object is returned which conforms to the list protocol. Calling " { $link car } ", " { $link cdr } " or " { $link nil? } " on this will evaluate elements as required." }
|
||||
{ $see-also leach lmap lmap-with ltake lappend lfrom lfrom-by lconcat lcartesian-product lcartesian-product* lcomp lcomp* lmerge } ;
|
||||
|
||||
HELP: lwhile
|
||||
{ $values { "list" "a cons object" } { "quot" "a quotation with stack effect ( X -- bool )" } { "result" "resulting cons object" } }
|
||||
{ $description "Outputs a lazy list containing the first items in the list as long as " { $snippet "quot" } " evaluates to t. No evaluation of the list elements occurs initially but a " { $link <lazy-while> } " object is returned with conforms to the list protocol. Calling " { $link car } ", " { $link cdr } " or " { $link nil? } " on this will evaluate elements as required." }
|
||||
{ $see-also luntil } ;
|
||||
|
||||
HELP: luntil
|
||||
{ $values { "list" "a cons object" } { "quot" "a quotation with stack effect ( X -- bool )" } { "result" "resulting cons object" } }
|
||||
{ $description "Outputs a lazy list containing the first items in the list until after " { $snippet "quot" } " evaluates to t. No evaluation of the list elements occurs initially but a " { $link <lazy-while> } " object is returned with conforms to the list protocol. Calling " { $link car } ", " { $link cdr } " or " { $link nil? } " on this will evaluate elements as required." }
|
||||
{ $see-also lwhile } ;
|
||||
|
||||
HELP: list>vector
|
||||
{ $values { "list" "a cons object" } { "vector" "the list converted to a vector" } }
|
||||
{ $description "Convert a list to a vector. If the list is a lazy infinite list then this will enter an infinite loop." }
|
||||
|
|
|
@ -206,6 +206,48 @@ M: lazy-take nil? ( lazy-take -- bool )
|
|||
M: lazy-take list? ( object -- bool )
|
||||
drop t ;
|
||||
|
||||
TUPLE: lazy-until cons quot ;
|
||||
|
||||
C: <lazy-until> lazy-until
|
||||
|
||||
: luntil ( list quot -- result )
|
||||
<lazy-until> ;
|
||||
|
||||
M: lazy-until car ( lazy-until -- car )
|
||||
lazy-until-cons car ;
|
||||
|
||||
M: lazy-until cdr ( lazy-until -- cdr )
|
||||
[ lazy-until-cons uncons ] keep lazy-until-quot
|
||||
rot over call [ 2drop nil ] [ luntil ] if ;
|
||||
|
||||
M: lazy-until nil? ( lazy-until -- bool )
|
||||
lazy-until-cons nil? ;
|
||||
|
||||
M: lazy-until list? ( lazy-until -- bool )
|
||||
drop t ;
|
||||
|
||||
TUPLE: lazy-while cons quot ;
|
||||
|
||||
C: <lazy-while> lazy-while
|
||||
|
||||
: lwhile ( list quot -- result )
|
||||
<lazy-while>
|
||||
;
|
||||
|
||||
M: lazy-while car ( lazy-while -- car )
|
||||
lazy-while-cons car ;
|
||||
|
||||
M: lazy-while cdr ( lazy-while -- cdr )
|
||||
dup lazy-while-cons cdr dup nil?
|
||||
[ 2drop nil ] [ swap lazy-while-quot lwhile ] if ;
|
||||
|
||||
M: lazy-while nil? ( lazy-while -- bool )
|
||||
dup lazy-while-cons nil?
|
||||
[ nip ] [ [ car ] keep lazy-while-quot call not ] if* ;
|
||||
|
||||
M: lazy-while list? ( lazy-while -- bool )
|
||||
drop t ;
|
||||
|
||||
TUPLE: lazy-subset cons quot ;
|
||||
|
||||
C: <lazy-subset> lazy-subset
|
||||
|
|
Loading…
Reference in New Issue