lists: first pass at some cleanup.

Now lists.lazy:lmap-lazy and lists.lazy:lappend-lazy names differentiate
from their non-lazy counterparts in lists.
locals-and-roots
John Benediktsson 2016-04-17 13:08:32 -07:00
parent 6b5b955a41
commit 42ae9ac015
10 changed files with 108 additions and 118 deletions

View File

@ -11,5 +11,5 @@ IN: lists.lazy.examples
: odds ( -- list ) 1 lfrom [ 2 mod 1 = ] lfilter ; : odds ( -- list ) 1 lfrom [ 2 mod 1 = ] lfilter ;
: powers-of-2 ( -- list ) 1 [ 2 * ] lfrom-by ; : powers-of-2 ( -- list ) 1 [ 2 * ] lfrom-by ;
: ones ( -- list ) 1 [ ] lfrom-by ; : ones ( -- list ) 1 [ ] lfrom-by ;
: squares ( -- list ) naturals [ dup * ] lazy-map ; : squares ( -- list ) naturals [ dup * ] lmap-lazy ;
: first-five-squares ( -- list ) 5 squares ltake list>array ; : first-five-squares ( -- list ) 5 squares ltake list>array ;

View File

@ -18,7 +18,7 @@ ARTICLE: "lists.lazy" "Lazy lists"
ARTICLE: { "lists.lazy" "combinators" } "Combinators for manipulating lazy lists" ARTICLE: { "lists.lazy" "combinators" } "Combinators for manipulating lazy lists"
"The following combinators create lazy lists from other lazy lists:" "The following combinators create lazy lists from other lazy lists:"
{ $subsections { $subsections
lazy-map lmap-lazy
lfilter lfilter
luntil luntil
lwhile lwhile
@ -50,7 +50,7 @@ ARTICLE: { "lists.lazy" "manipulation" } "Manipulating lazy lists"
"To make new lazy lists from old ones:" "To make new lazy lists from old ones:"
{ $subsections { $subsections
<memoized-cons> <memoized-cons>
lappend lappend-lazy
lconcat lconcat
lcartesian-product lcartesian-product
lcartesian-product* lcartesian-product*
@ -82,31 +82,31 @@ HELP: <memoized-cons>
{ $description "Constructs a cons object that wraps an existing cons object. Requests for the car, cdr and nil? will be remembered after the first call, and the previous result returned on subsequent calls." } { $description "Constructs a cons object that wraps an existing cons object. Requests for the car, cdr and nil? will be remembered after the first call, and the previous result returned on subsequent calls." }
{ $see-also cons car cdr nil nil? } ; { $see-also cons car cdr nil nil? } ;
{ lazy-map ltake lfilter lappend lfrom lfrom-by lconcat lcartesian-product lcartesian-product* lcomp lcomp* lmerge lwhile luntil } related-words { lmap-lazy ltake lfilter lappend-lazy lfrom lfrom-by lconcat lcartesian-product lcartesian-product* lcomp lcomp* lmerge lwhile luntil } related-words
HELP: lazy-map HELP: lmap-lazy
{ $values { "list" "a cons object" } { "quot" { $quotation ( obj -- X ) } } { "result" "resulting cons object" } } { $values { "list" "a cons object" } { "quot" { $quotation ( obj -- X ) } } { "result" "resulting cons object" } }
{ $description "Perform a similar functionality to that of the " { $link map } " word, but in a lazy manner. No evaluation of the list elements occurs initially but a " { $link <lazy-map-state> } " object is returned which conforms to the list protocol. Calling " { $link car } ", " { $link cdr } " or " { $link nil? } " on this will evaluate elements as required." } ; { $description "Perform a similar functionality to that of the " { $link map } " word, but in a lazy manner. No evaluation of the list elements occurs initially but a " { $link lazy-map } " object is returned which conforms to the list protocol. Calling " { $link car } ", " { $link cdr } " or " { $link nil? } " on this will evaluate elements as required." } ;
HELP: ltake HELP: ltake
{ $values { "n" "a non negative integer" } { "list" "a cons object" } { "result" "resulting cons object" } } { $values { "n" "a non negative integer" } { "list" "a cons object" } { "result" "resulting cons object" } }
{ $description "Outputs a lazy list containing the first n items in the list. This is done a lazy manner. No evaluation of the list elements occurs initially but a " { $link <lazy-take> } " object is returned which conforms to the list protocol. Calling " { $link car } ", " { $link cdr } " or " { $link nil? } " on this will evaluate elements as required." } ; { $description "Outputs a lazy list containing the first n items in the list. This is done a lazy manner. No evaluation of the list elements occurs initially but a " { $link lazy-take } " object is returned which conforms to the list protocol. Calling " { $link car } ", " { $link cdr } " or " { $link nil? } " on this will evaluate elements as required." } ;
HELP: lfilter HELP: lfilter
{ $values { "list" "a cons object" } { "quot" { $quotation ( -- X ) } } { "result" "resulting cons object" } } { $values { "list" "a cons object" } { "quot" { $quotation ( -- X ) } } { "result" "resulting cons object" } }
{ $description "Perform a similar functionality to that of the " { $link filter } " word, but in a lazy manner. No evaluation of the list elements occurs initially but a " { $link <lazy-filter> } " object is returned which conforms to the list protocol. Calling " { $link car } ", " { $link cdr } " or " { $link nil? } " on this will evaluate elements as required." } ; { $description "Perform a similar functionality to that of the " { $link filter } " word, but in a lazy manner. No evaluation of the list elements occurs initially but a " { $link lazy-filter } " object is returned which conforms to the list protocol. Calling " { $link car } ", " { $link cdr } " or " { $link nil? } " on this will evaluate elements as required." } ;
HELP: lwhile HELP: lwhile
{ $values { "list" "a cons object" } { "quot" { $quotation ( x -- ? ) } } { "result" "resulting cons object" } } { $values { "list" "a cons object" } { "quot" { $quotation ( x -- ? ) } } { "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." } ; { $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." } ;
HELP: luntil HELP: luntil
{ $values { "list" "a cons object" } { "quot" { $quotation ( x -- ? ) } } { "result" "resulting cons object" } } { $values { "list" "a cons object" } { "quot" { $quotation ( x -- ? ) } } { "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." } ; { $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." } ;
HELP: lappend HELP: lappend-lazy
{ $values { "list1" "a cons object" } { "list2" "a cons object" } { "result" "a lazy list of list2 appended to list1" } } { $values { "list1" "a cons object" } { "list2" "a cons object" } { "result" "a lazy list of list2 appended to list1" } }
{ $description "Perform a similar functionality to that of the " { $link append } " word, but in a lazy manner. No evaluation of the list elements occurs initially but a " { $link <lazy-append> } " object is returned which conforms to the list protocol. Calling " { $link car } ", " { $link cdr } " or " { $link nil? } " on this will evaluate elements as required. Successive calls to " { $link cdr } " will iterate through list1, followed by list2." } ; { $description "Perform a similar functionality to that of the " { $link append } " word, but in a lazy manner. No evaluation of the list elements occurs initially but a " { $link lazy-append } " object is returned which conforms to the list protocol. Calling " { $link car } ", " { $link cdr } " or " { $link nil? } " on this will evaluate elements as required. Successive calls to " { $link cdr } " will iterate through list1, followed by list2." } ;
HELP: lfrom-by HELP: lfrom-by
{ $values { "n" integer } { "quot" { $quotation ( n -- o ) } } { "lazy-from-by" "a lazy list of integers" } } { $values { "n" integer } { "quot" { $quotation ( n -- o ) } } { "lazy-from-by" "a lazy list of integers" } }
@ -121,7 +121,7 @@ HELP: sequence-tail>list
{ $description "Convert the sequence into a list, starting from " { $snippet "index" } "." } { $description "Convert the sequence into a list, starting from " { $snippet "index" } "." }
{ $see-also >list } ; { $see-also >list } ;
{ leach foldl lazy-map ltake lfilter lappend lfrom lfrom-by lconcat lcartesian-product lcartesian-product* lcomp lcomp* lmerge lwhile luntil } related-words { leach foldl lmap-lazy ltake lfilter lappend-lazy lfrom lfrom-by lconcat lcartesian-product lcartesian-product* lcomp lcomp* lmerge lwhile luntil } related-words
HELP: lconcat HELP: lconcat
{ $values { "list" "a list of lists" } { "result" "a list" } } { $values { "list" "a list of lists" } { "result" "a list" } }

View File

@ -25,7 +25,7 @@ IN: lists.lazy.tests
] unit-test ] unit-test
{ { 4 5 6 } } [ { { 4 5 6 } } [
3 { 1 2 3 } >list [ + ] with lazy-map list>array 3 { 1 2 3 } >list [ + ] with lmap-lazy list>array
] unit-test ] unit-test
{ { 1 2 4 8 16 } } [ { { 1 2 4 8 16 } } [

View File

@ -4,31 +4,27 @@ USING: accessors arrays combinators io kernel lists math
promises quotations sequences ; promises quotations sequences ;
IN: lists.lazy IN: lists.lazy
M: promise car ( promise -- car ) M: promise car force car ;
force car ;
M: promise cdr ( promise -- cdr ) M: promise cdr force cdr ;
force cdr ;
M: promise nil? ( cons -- ? ) M: promise nil? force nil? ;
force nil? ;
! Both 'car' and 'cdr' are promises TUPLE: lazy-cons-state
TUPLE: lazy-cons-state car cdr ; { car promise }
{ cdr promise } ;
C: <lazy-cons-state> lazy-cons-state
: lazy-cons ( car cdr -- promise ) : lazy-cons ( car cdr -- promise )
[ T{ promise f f t f } clone ] 2dip [ <promise> ] bi@ <lazy-cons-state>
[ <promise> ] bi@ \ lazy-cons-state boa [ f t ] dip promise boa ;
>>value ;
M: lazy-cons-state car ( lazy-cons -- car ) M: lazy-cons-state car car>> force ;
car>> force ;
M: lazy-cons-state cdr ( lazy-cons -- cdr ) M: lazy-cons-state cdr cdr>> force ;
cdr>> force ;
M: lazy-cons-state nil? ( lazy-cons -- ? ) M: lazy-cons-state nil? nil eq? ;
nil eq? ;
: 1lazy-list ( a -- lazy-cons ) : 1lazy-list ( a -- lazy-cons )
[ nil ] lazy-cons ; [ nil ] lazy-cons ;
@ -41,49 +37,47 @@ M: lazy-cons-state nil? ( lazy-cons -- ? )
TUPLE: memoized-cons original car cdr nil? ; TUPLE: memoized-cons original car cdr nil? ;
: not-memoized ( -- obj ) { } ; SYMBOL: +not-memoized+
: not-memoized? ( obj -- ? ) not-memoized eq? ;
: <memoized-cons> ( cons -- memoized-cons ) : <memoized-cons> ( cons -- memoized-cons )
not-memoized not-memoized not-memoized +not-memoized+ +not-memoized+ +not-memoized+
memoized-cons boa ; memoized-cons boa ;
M: memoized-cons car ( memoized-cons -- car ) M: memoized-cons car
dup car>> not-memoized? [ dup car>> +not-memoized+ eq? [
dup original>> car [ >>car drop ] keep dup original>> car [ >>car drop ] keep
] [ ] [
car>> car>>
] if ; ] if ;
M: memoized-cons cdr ( memoized-cons -- cdr ) M: memoized-cons cdr
dup cdr>> not-memoized? [ dup cdr>> +not-memoized+ eq? [
dup original>> cdr [ >>cdr drop ] keep dup original>> cdr [ >>cdr drop ] keep
] [ ] [
cdr>> cdr>>
] if ; ] if ;
M: memoized-cons nil? ( memoized-cons -- ? ) M: memoized-cons nil?
dup nil?>> not-memoized? [ dup nil?>> +not-memoized+ eq? [
dup original>> nil? [ >>nil? drop ] keep dup original>> nil? [ >>nil? drop ] keep
] [ ] [
nil?>> nil?>>
] if ; ] if ;
TUPLE: lazy-map-state cons quot ; TUPLE: lazy-map cons quot ;
C: <lazy-map-state> lazy-map-state C: <lazy-map> lazy-map
: lazy-map ( list quot -- result ) : lmap-lazy ( list quot -- result )
over nil? [ 2drop nil ] [ <lazy-map-state> <memoized-cons> ] if ; over nil? [ 2drop nil ] [ <lazy-map> <memoized-cons> ] if ;
M: lazy-map-state car ( lazy-map -- car ) M: lazy-map car
[ cons>> car ] [ quot>> call( old -- new ) ] bi ; [ cons>> car ] [ quot>> call( old -- new ) ] bi ;
M: lazy-map-state cdr ( lazy-map -- cdr ) M: lazy-map cdr
[ cons>> cdr ] [ quot>> lazy-map ] bi ; [ cons>> cdr ] [ quot>> lmap-lazy ] bi ;
M: lazy-map-state nil? ( lazy-map -- ? ) M: lazy-map nil?
cons>> nil? ; cons>> nil? ;
TUPLE: lazy-take n cons ; TUPLE: lazy-take n cons ;
@ -93,14 +87,13 @@ C: <lazy-take> lazy-take
: ltake ( n list -- result ) : ltake ( n list -- result )
over zero? [ 2drop nil ] [ <lazy-take> ] if ; over zero? [ 2drop nil ] [ <lazy-take> ] if ;
M: lazy-take car ( lazy-take -- car ) M: lazy-take car
cons>> car ; cons>> car ;
M: lazy-take cdr ( lazy-take -- cdr ) M: lazy-take cdr
[ n>> 1 - ] keep [ n>> 1 - ] [ cons>> cdr ltake ] bi ;
cons>> cdr ltake ;
M: lazy-take nil? ( lazy-take -- ? ) M: lazy-take nil?
dup n>> zero? [ drop t ] [ cons>> nil? ] if ; dup n>> zero? [ drop t ] [ cons>> nil? ] if ;
TUPLE: lazy-until cons quot ; TUPLE: lazy-until cons quot ;
@ -110,15 +103,15 @@ C: <lazy-until> lazy-until
: luntil ( list quot -- result ) : luntil ( list quot -- result )
over nil? [ drop ] [ <lazy-until> ] if ; over nil? [ drop ] [ <lazy-until> ] if ;
M: lazy-until car ( lazy-until -- car ) M: lazy-until car
cons>> car ; cons>> car ;
M: lazy-until cdr ( lazy-until -- cdr ) M: lazy-until cdr
[ [ cons>> cdr ] [ quot>> ] bi ] [ [ cons>> cdr ] [ quot>> ] bi ]
[ [ cons>> car ] [ quot>> ] bi call( elt -- ? ) ] bi [ [ cons>> car ] [ quot>> ] bi call( elt -- ? ) ] bi
[ 2drop nil ] [ luntil ] if ; [ 2drop nil ] [ luntil ] if ;
M: lazy-until nil? ( lazy-until -- ? ) M: lazy-until nil?
drop f ; drop f ;
TUPLE: lazy-while cons quot ; TUPLE: lazy-while cons quot ;
@ -128,13 +121,13 @@ C: <lazy-while> lazy-while
: lwhile ( list quot -- result ) : lwhile ( list quot -- result )
over nil? [ drop ] [ <lazy-while> ] if ; over nil? [ drop ] [ <lazy-while> ] if ;
M: lazy-while car ( lazy-while -- car ) M: lazy-while car
cons>> car ; cons>> car ;
M: lazy-while cdr ( lazy-while -- cdr ) M: lazy-while cdr
[ cons>> cdr ] keep quot>> lwhile ; [ cons>> cdr ] keep quot>> lwhile ;
M: lazy-while nil? ( lazy-while -- ? ) M: lazy-while nil?
[ car ] keep quot>> call( elt -- ? ) not ; [ car ] keep quot>> call( elt -- ? ) not ;
TUPLE: lazy-filter cons quot ; TUPLE: lazy-filter cons quot ;
@ -144,47 +137,47 @@ C: <lazy-filter> lazy-filter
: lfilter ( list quot -- result ) : lfilter ( list quot -- result )
over nil? [ 2drop nil ] [ <lazy-filter> <memoized-cons> ] if ; over nil? [ 2drop nil ] [ <lazy-filter> <memoized-cons> ] if ;
<PRIVATE
: car-filter? ( lazy-filter -- ? ) : car-filter? ( lazy-filter -- ? )
[ cons>> car ] [ quot>> ] bi call( elt -- ? ) ; [ cons>> car ] [ quot>> ] bi call( elt -- ? ) ;
: skip ( lazy-filter -- ) : skip ( lazy-filter -- )
dup cons>> cdr >>cons drop ; dup cons>> cdr >>cons drop ;
M: lazy-filter car ( lazy-filter -- car ) PRIVATE>
M: lazy-filter car
dup car-filter? [ cons>> ] [ dup skip ] if car ; dup car-filter? [ cons>> ] [ dup skip ] if car ;
M: lazy-filter cdr ( lazy-filter -- cdr ) M: lazy-filter cdr
dup car-filter? [ dup car-filter? [
[ cons>> cdr ] [ quot>> ] bi lfilter [ cons>> cdr ] [ quot>> ] bi lfilter
] [ ] [
dup skip cdr dup skip cdr
] if ; ] if ;
M: lazy-filter nil? ( lazy-filter -- ? ) M: lazy-filter nil?
dup cons>> nil? [ {
drop t { [ dup cons>> nil? ] [ drop t ] }
] [ { [ dup car-filter? ] [ drop f ] }
dup car-filter? [ [ dup skip nil? ]
drop f } cond ;
] [
dup skip nil?
] if
] if ;
TUPLE: lazy-append list1 list2 ; TUPLE: lazy-append list1 list2 ;
C: <lazy-append> lazy-append C: <lazy-append> lazy-append
: lappend ( list1 list2 -- result ) : lappend-lazy ( list1 list2 -- result )
over nil? [ nip ] [ <lazy-append> ] if ; over nil? [ nip ] [ <lazy-append> ] if ;
M: lazy-append car ( lazy-append -- car ) M: lazy-append car
list1>> car ; list1>> car ;
M: lazy-append cdr ( lazy-append -- cdr ) M: lazy-append cdr
[ list1>> cdr ] [ list2>> ] bi lappend ; [ list1>> cdr ] [ list2>> ] bi lappend-lazy ;
M: lazy-append nil? ( lazy-append -- ? ) M: lazy-append nil?
drop f ; drop f ;
TUPLE: lazy-from-by n quot ; TUPLE: lazy-from-by n quot ;
@ -194,14 +187,13 @@ TUPLE: lazy-from-by n quot ;
: lfrom ( n -- list ) : lfrom ( n -- list )
[ 1 + ] lfrom-by ; [ 1 + ] lfrom-by ;
M: lazy-from-by car ( lazy-from-by -- car ) M: lazy-from-by car
n>> ; n>> ;
M: lazy-from-by cdr ( lazy-from-by -- cdr ) M: lazy-from-by cdr
[ n>> ] keep [ n>> ] [ quot>> ] bi [ call( old -- new ) ] keep lfrom-by ;
quot>> [ call( old -- new ) ] keep lfrom-by ;
M: lazy-from-by nil? ( lazy-from-by -- ? ) M: lazy-from-by nil?
drop f ; drop f ;
TUPLE: lazy-zip list1 list2 ; TUPLE: lazy-zip list1 list2 ;
@ -209,17 +201,17 @@ TUPLE: lazy-zip list1 list2 ;
C: <lazy-zip> lazy-zip C: <lazy-zip> lazy-zip
: lzip ( list1 list2 -- lazy-zip ) : lzip ( list1 list2 -- lazy-zip )
over nil? over nil? or over nil? over nil? or
[ 2drop nil ] [ <lazy-zip> ] if ; [ 2drop nil ] [ <lazy-zip> ] if ;
M: lazy-zip car ( lazy-zip -- car ) M: lazy-zip car
[ list1>> car ] keep list2>> car 2array ; [ list1>> car ] keep list2>> car 2array ;
M: lazy-zip cdr ( lazy-zip -- cdr ) M: lazy-zip cdr
[ list1>> cdr ] keep list2>> cdr lzip ; [ list1>> cdr ] keep list2>> cdr lzip ;
M: lazy-zip nil? ( lazy-zip -- ? ) M: lazy-zip nil?
drop f ; drop f ;
TUPLE: sequence-cons index seq ; TUPLE: sequence-cons index seq ;
@ -232,13 +224,13 @@ C: <sequence-cons> sequence-cons
<sequence-cons> <sequence-cons>
] if ; ] if ;
M: sequence-cons car ( sequence-cons -- car ) M: sequence-cons car
[ index>> ] [ seq>> nth ] bi ; [ index>> ] [ seq>> nth ] bi ;
M: sequence-cons cdr ( sequence-cons -- cdr ) M: sequence-cons cdr
[ index>> 1 + ] [ seq>> sequence-tail>list ] bi ; [ index>> 1 + ] [ seq>> sequence-tail>list ] bi ;
M: sequence-cons nil? ( sequence-cons -- ? ) M: sequence-cons nil?
drop f ; drop f ;
M: sequence >list 0 swap sequence-tail>list ; M: sequence >list 0 swap sequence-tail>list ;
@ -255,17 +247,17 @@ DEFER: lconcat
: lconcat ( list -- result ) : lconcat ( list -- result )
dup nil? [ drop nil ] [ uncons (lconcat) ] if ; dup nil? [ drop nil ] [ uncons (lconcat) ] if ;
M: lazy-concat car ( lazy-concat -- car ) M: lazy-concat car
car>> car ; car>> car ;
M: lazy-concat cdr ( lazy-concat -- cdr ) M: lazy-concat cdr
[ car>> cdr ] keep cdr>> (lconcat) ; [ car>> cdr ] keep cdr>> (lconcat) ;
M: lazy-concat nil? ( lazy-concat -- ? ) M: lazy-concat nil?
dup car>> nil? [ cdr>> nil? ] [ drop f ] if ; dup car>> nil? [ cdr>> nil? ] [ drop f ] if ;
: lcartesian-product ( list1 list2 -- result ) : lcartesian-product ( list1 list2 -- result )
swap [ swap [ 2array ] with lazy-map ] with lazy-map lconcat ; swap [ swap [ 2array ] with lmap-lazy ] with lmap-lazy lconcat ;
: lcartesian-product* ( lists -- result ) : lcartesian-product* ( lists -- result )
dup nil? [ dup nil? [
@ -274,15 +266,15 @@ M: lazy-concat nil? ( lazy-concat -- ? )
uncons uncons
[ car lcartesian-product ] [ cdr ] bi [ car lcartesian-product ] [ cdr ] bi
list>array swap [ list>array swap [
swap [ swap [ suffix ] with lazy-map ] with lazy-map lconcat swap [ swap [ suffix ] with lmap-lazy ] with lmap-lazy lconcat
] reduce ] reduce
] if ; ] if ;
: lcomp ( list quot -- result ) : lcomp ( list quot -- result )
[ lcartesian-product* ] dip lazy-map ; [ lcartesian-product* ] dip lmap-lazy ;
: lcomp* ( list guards quot -- result ) : lcomp* ( list guards quot -- result )
[ [ lcartesian-product* ] dip [ lfilter ] each ] dip lazy-map ; [ [ lcartesian-product* ] dip [ lfilter ] each ] dip lmap-lazy ;
DEFER: lmerge DEFER: lmerge
@ -312,7 +304,7 @@ C: <lazy-io> lazy-io
: llines ( stream -- result ) : llines ( stream -- result )
f f [ stream-readln ] <lazy-io> ; f f [ stream-readln ] <lazy-io> ;
M: lazy-io car ( lazy-io -- car ) M: lazy-io car
dup car>> [ dup car>> [
nip nip
] [ ] [
@ -320,21 +312,19 @@ M: lazy-io car ( lazy-io -- car )
call( stream -- value ) [ >>car ] [ drop nil ] if* call( stream -- value ) [ >>car ] [ drop nil ] if*
] if* ; ] if* ;
M: lazy-io cdr ( lazy-io -- cdr ) M: lazy-io cdr
dup cdr>> dup [ dup cdr>> dup [
nip nip
] [ ] [
drop dup drop dup [ stream>> ] [ quot>> ] [ car ] tri
[ stream>> ] [
[ quot>> ]
[ car ] tri [
[ f f ] dip <lazy-io> [ >>cdr drop ] keep [ f f ] dip <lazy-io> [ >>cdr drop ] keep
] [ ] [
3drop nil 3drop nil
] if ] if
] if ; ] if ;
M: lazy-io nil? ( lazy-io -- ? ) M: lazy-io nil?
car nil? ; car nil? ;
INSTANCE: sequence-cons list INSTANCE: sequence-cons list
@ -343,7 +333,7 @@ INSTANCE: promise list
INSTANCE: lazy-io list INSTANCE: lazy-io list
INSTANCE: lazy-concat list INSTANCE: lazy-concat list
INSTANCE: lazy-cons-state list INSTANCE: lazy-cons-state list
INSTANCE: lazy-map-state list INSTANCE: lazy-map list
INSTANCE: lazy-take list INSTANCE: lazy-take list
INSTANCE: lazy-append list INSTANCE: lazy-append list
INSTANCE: lazy-from-by list INSTANCE: lazy-from-by list

View File

@ -159,7 +159,7 @@ HELP: cadr
HELP: lappend HELP: lappend
{ $values { "list1" list } { "list2" list } { "newlist" list } } { $values { "list1" list } { "list2" list } { "newlist" list } }
{ $description "Appends the two lists to form a new list. The first list must be finite. The result is a strict cons cell, and the first list is exausted." } ; { $description "Appends the two lists to form a new list. The first list must be finite. The result is a strict cons cell, and the first list is exhausted." } ;
HELP: lcut HELP: lcut
{ $values { "list" list } { "index" integer } { "before" cons } { "after" cons } } { $values { "list" list } { "index" integer } { "before" cons } { "after" cons } }

View File

@ -42,7 +42,7 @@ M: object nil? drop f ;
: cadr ( list -- elt ) cdr car ; inline : cadr ( list -- elt ) cdr car ; inline
: 2car ( list -- car caar ) [ car ] [ cadr ] bi ; inline : 2car ( list -- car cadr ) [ car ] [ cadr ] bi ; inline
: 3car ( list -- car cadr caddr ) [ car ] [ cadr ] [ cdr cadr ] tri ; inline : 3car ( list -- car cadr caddr ) [ car ] [ cadr ] [ cdr cadr ] tri ; inline

View File

@ -127,7 +127,7 @@ M: list-monad fail 2drop nil ;
M: list monad-of drop list-monad ; M: list monad-of drop list-monad ;
M: list >>= '[ _ swap lazy-map lconcat ] ; M: list >>= '[ _ swap lmap-lazy lconcat ] ;
! State ! State
SINGLETON: state-monad SINGLETON: state-monad

View File

@ -151,8 +151,8 @@ TUPLE: and-parser parsers ;
[ parsed>> ] dip [ parsed>> ] dip
[ parsed>> 2array ] keep [ parsed>> 2array ] keep
unparsed>> <parse-result> unparsed>> <parse-result>
] with lazy-map ] with lmap-lazy
] with lazy-map lconcat ; ] with lmap-lazy lconcat ;
M: and-parser parse ( input parser -- list ) M: and-parser parse ( input parser -- list )
! Parse 'input' by sequentially combining the ! Parse 'input' by sequentially combining the
@ -175,7 +175,7 @@ M: or-parser parse ( input parser1 -- list )
! of parser1 and parser2 being applied to the same ! of parser1 and parser2 being applied to the same
! input. This implements the choice parsing operator. ! input. This implements the choice parsing operator.
parsers>> sequence>list parsers>> sequence>list
[ parse ] with lazy-map lconcat ; [ parse ] with lmap-lazy lconcat ;
: trim-head-slice ( string -- string ) : trim-head-slice ( string -- string )
! Return a new string without any leading whitespace ! Return a new string without any leading whitespace
@ -220,7 +220,7 @@ M: apply-parser parse ( input parser -- result )
-rot parse [ -rot parse [
[ parsed>> swap call ] keep [ parsed>> swap call ] keep
unparsed>> <parse-result> unparsed>> <parse-result>
] with lazy-map ; ] with lmap-lazy ;
TUPLE: some-parser p1 ; TUPLE: some-parser p1 ;

View File

@ -60,7 +60,7 @@ IN: project-euler.065
: (e-frac) ( -- seq ) : (e-frac) ( -- seq )
2 lfrom [ 2 lfrom [
dup 3 mod zero? [ 3 / 2 * ] [ drop 1 ] if dup 3 mod zero? [ 3 / 2 * ] [ drop 1 ] if
] lazy-map ; ] lmap-lazy ;
: e-frac ( n -- n ) : e-frac ( n -- n )
1 - (e-frac) ltake list>array reverse 0 1 - (e-frac) ltake list>array reverse 0

View File

@ -32,6 +32,6 @@ IN: rosetta-code.hamming-lazy
:: hamming ( -- hamming ) :: hamming ( -- hamming )
f :> h! f :> h!
[ 1 ] [ [ 1 ] [
h 2 3 5 [ '[ _ * ] lazy-map ] tri-curry@ tri h 2 3 5 [ '[ _ * ] lmap-lazy ] tri-curry@ tri
sort-merge sort-merge sort-merge sort-merge
] lazy-cons h! h ; ] lazy-cons h! h ;