lazy-lists: more refactoring

darcs
chris.double 2006-09-13 01:32:23 +00:00
parent 31c34547bc
commit 84ad813665
3 changed files with 137 additions and 57 deletions

View File

@ -1,31 +1,40 @@
! Updated by Matthew Willis, July 2006
!
! Copyright (C) 2004 Chris Double.
!
! Redistribution and use in source and binary forms, with or without
! modification, are permitted provided that the following conditions are met:
!
! 1. Redistributions of source code must retain the above copyright notice,
! this list of conditions and the following disclaimer.
!
! 2. Redistributions in binary form must reproduce the above copyright notice,
! this list of conditions and the following disclaimer in the documentation
! and/or other materials provided with the distribution.
!
! THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
! INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
! FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
! DEVELOPERS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
! SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
! PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
! OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
! WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
! OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
! ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
! See http://factorcode.org/license.txt for BSD license.
!
! Updated by Matthew Willis, July 2006
USING: kernel sequences math vectors arrays namespaces ;
USING: kernel sequences math vectors arrays namespaces generic ;
IN: lazy-lists
TUPLE: cons car cdr ;
GENERIC: car ( cons -- car )
GENERIC: cdr ( cons -- cdr )
C: cons ( car cdr -- list )
[ set-cons-cdr ] keep
[ set-cons-car ] keep ;
M: cons car ( cons -- car )
cons-car ;
M: cons cdr ( cons -- cdr )
cons-cdr ;
: nil ( -- list )
{ } ;
: nil? ( list -- bool )
{ } = ;
: cons ( car cdr -- list )
<cons> ;
: 1list ( obj -- cons )
nil <cons> ;
: 2list ( obj obj -- cons )
nil <cons> <cons> ;
TUPLE: promise quot forced? value ;
C: promise ( quot -- promise ) [ set-promise-quot ] keep ;
@ -41,36 +50,31 @@ C: promise ( quot -- promise ) [ set-promise-quot ] keep ;
promise-value ;
! Both 'car' and 'cdr' are promises
TUPLE: cons car cdr ;
: lazy-cons ( car cdr -- promise )
>r <promise> r> <promise> <cons>
T{ promise f f t f } clone [ set-promise-value ] keep ;
: nil ( -- list )
#! The nil lazy list.
{ } ;
M: promise car ( promise -- car )
force car force ;
: nil? ( list -- bool )
#! Is the given lazy cons the nil value
{ } = ;
M: promise cdr ( promise -- cdr )
force cdr force ;
: car ( list -- car )
#! Return the value of the head of the lazy list.
cons-car force ;
DEFER: lunit
DEFER: lnth
TUPLE: list ;
: cdr ( list -- cdr )
#! Return the rest of the lazy list.
#! This is itself a lazy list.
cons-cdr force ;
: 1lazy-list ( a -- lazy-cons )
[ nil ] lazy-cons ;
: cons ( car cdr -- list )
#! Given a car and cdr, both quotations, return a cons.
>r <promise> r> <promise> <cons> ;
: 2lazy-list ( a b -- lazy-cons )
1lazy-list unit lazy-cons ;
: lunit ( obj -- list )
#! Given a value produce a lazy list containing that value.
unit [ nil ] cons ;
: 3lazy-list ( a b c -- lazy-cons )
2lazy-list unit lazy-cons ;
: lnth ( n list -- value )
#! Return the nth item in a lazy list
swap [ cdr ] times car ;
: lnth ( n list -- elt )
swap [ cdr ] times car ;
: uncons ( cons -- car cdr )
#! Return the car and cdr of the lazy list
@ -79,23 +83,34 @@ TUPLE: cons car cdr ;
: 2curry ( a b quot -- quot )
curry curry ;
TUPLE: lazy-map cons quot ;
: lmap ( list quot -- list )
#! Return a lazy list containing the collected result of calling
#! quot on the original lazy list.
over nil? [
drop
] [
2dup [ >r cdr r> lmap ] 2curry >r [ >r car r> call ] 2curry r> cons
] if ;
over nil? [ drop ] [ <lazy-map> ] if ;
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 ;
: ltake ( n list -- list )
#! Return a lazy list containing the first n items from
#! the original lazy list.
over zero? [
2drop nil
] [
2dup [ >r 1- r> cdr ltake ] 2curry >r [ nip car ] 2curry r> cons
] if ;
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 ;
DEFER: lsubset
: (lsubset) ( list pred -- list )
@ -147,6 +162,8 @@ DEFER: backwards-vector>list
dup empty? [ drop nil ]
[ dup pop swap backwards-vector>list cons ] if ;
DEFER: force-promise
: backwards-vector>list ( vector -- list )
[ , \ (backwards-vector>list) , ] force-promise ;

View File

@ -0,0 +1,59 @@
! Copyright (C) 2006 Chris Double.
! See http://factorcode.org/license.txt for BSD license.
USING: help lazy-lists ;
HELP: <promise>
{ $values { "quot" "a quotation with stack effect ( -- X )" } { "promise" "a promise object" } }
{ $description "Creates a promise to return a value. When forced this quotation is called and the value returned. The value is memorised so that calling " { $link force } " again does not call the quotation again, instead the previous value is returned directly." }
{ $see-also force } ;
HELP: force
{ $values { "promise" "a promise object" } { "value" "a factor object" } }
{ $description "Calls the quotation associated with the promise if it has not been called before, and returns the value. If the promise has been forced previously, returns the value from the previous call." }
{ $see-also <promise> } ;
HELP: <cons>
{ $values { "car" "A promise for the head of the lazy list" } { "cdr" "A promise for the tail of the lazy list" } { "cons" "a cons object" } }
{ $description "Constructs a lazy cons cell. The car and cdr are promises that, when forced, provide the non-lazy values." }
{ $see-also car cdr nil } ;
HELP: car
{ $values { "cons" "a cons object" } { "car" "the first item in the list" } }
{ $description "Returns the first item in the list. This causes the item to be evaluated." }
{ $see-also <cons> car cdr } ;
HELP: cdr
{ $values { "cons" "a cons object" } { "cdr" "a cons object" } }
{ $description "Returns the tail of the list." }
{ $see-also <cons> car cdr } ;
HELP: cons
{ $values { "car" "a quotation with stack effect ( -- X )" } { "cdr" "a quotation with stack effect ( -- cons )" } { "cons" "the resulting cons object" } }
{ $description "Constructs a cons object for a lazy list from two quotations. The 'car' quotation should return the head of the list, and the 'cons' quotation the tail when called." }
{ $see-also <cons> car cdr } ;
HELP: nil
{ $values { "array" "An empty array" } }
{ $description "Returns a representation of an empty lazy list" }
{ $see-also <cons> car cdr nil? } ;
HELP: nil?
{ $values { "cons" "a cons object" } }
{ $description "Return true if the cons object is the nil list." }
{ $see-also nil } ;
HELP: <list>
{ $values { "car" "the car of the list" } { "cdr" "a <cons> object for the cdr of the list" } { "list" "a lazy list" } }
{ $description "Constructs a lazy list where the car is already forced and the cdr is an already forced list." }
{ $see-also car cdr } ;
HELP: 1list
{ $values { "obj" "an object" } { "list" "a list" } }
{ $description "Create a list with 1 element." }
{ $see-also <list> 2list } ;
HELP: 2list
{ $values { "a" "an object" } { "b" "an object" } { "list" "a list" } }
{ $description "Create a list with 2 elements." }
{ $see-also <list> 1list } ;

View File

@ -1,5 +1,9 @@
! Copyright (C) 2006 Chris Double.
! See http://factorcode.org/license.txt for BSD license.
PROVIDE: contrib/lazy-lists {
"lists.factor"
"lists.facts"
"examples.factor"
} {
"test/lists.factor"