lazy-lists: stick with single 'lcomp' word and 'lcomp*' to include guards

chris.double 2006-10-05 03:00:17 +00:00
parent e252f06e7b
commit f84b6ba3ce
2 changed files with 27 additions and 23 deletions

View File

@ -302,8 +302,8 @@ M: lazy-zip nil? ( lazy-zip -- bool )
] reduce ] reduce
] if ; ] if ;
: lcomp2 ( list1 list2 quot -- list ) : lcomp ( list quot -- result )
>r lcartesian-product r> swap [ swap >r first2 r> call ] lmap-with ; >r lcartesian-product* r> lmap ;
: lcomp3 ( list1 list2 list3 quot -- list ) : lcomp* ( list guards quot -- result )
>r 3array seq>list lcartesian-product* r> swap [ swap >r first3 r> call ] lmap-with ; >r >r lcartesian-product* r> [ lsubset ] each r> lmap ;

View File

@ -91,27 +91,27 @@ HELP: uncons
HELP: leach HELP: leach
{ $values { "list" "a cons object" } { "quot" "a quotation with stack effect ( obj -- )" } } { $values { "list" "a cons object" } { "quot" "a quotation with stack effect ( obj -- )" } }
{ $description "Call the quotation for each item in the list." } { $description "Call the quotation for each item in the list." }
{ $see-also lmap lmap-with ltake lsubset lappend lfrom lfrom-by lconcat lcartesian-product lcartesian-product* } ; { $see-also lmap lmap-with ltake lsubset lappend lfrom lfrom-by lconcat lcartesian-product lcartesian-product* lcomp lcomp* } ;
HELP: lmap HELP: lmap
{ $values { "list" "a cons object" } { "quot" "a quotation with stack effect ( obj -- X )" } { "result" "resulting cons object" } } { $values { "list" "a cons object" } { "quot" "a quotation with stack effect ( 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> } " 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." }
{ $see-also leach ltake lsubset lappend lmap-with lfrom lfrom-by lconcat lcartesian-product lcartesian-product* } ; { $see-also leach ltake lsubset lappend lmap-with lfrom lfrom-by lconcat lcartesian-product lcartesian-product* lcomp lcomp* } ;
HELP: lmap-with HELP: lmap-with
{ $values { "value" "an object" } { "list" "a cons object" } { "quot" "a quotation with stack effect ( obj elt -- X )" } { "result" "resulting cons object" } } { $values { "value" "an object" } { "list" "a cons object" } { "quot" "a quotation with stack effect ( obj elt -- X )" } { "result" "resulting cons object" } }
{ $description "Variant of " { $link lmap } " which pushes a retained object on each invocation of the quotation." } { $description "Variant of " { $link lmap } " which pushes a retained object on each invocation of the quotation." }
{ $see-also leach ltake lsubset lappend lmap lfrom lfrom-by lconcat lcartesian-product lcartesian-product* } ; { $see-also leach ltake lsubset lappend lmap lfrom lfrom-by lconcat lcartesian-product lcartesian-product* lcomp lcomp* } ;
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." }
{ $see-also leach lmap lmap-with lsubset lappend lfrom lfrom-by lconcat lcartesian-product lcartesian-product* } ; { $see-also leach lmap lmap-with lsubset lappend lfrom lfrom-by lconcat lcartesian-product lcartesian-product* lcomp lcomp* } ;
HELP: lsubset HELP: lsubset
{ $values { "list" "a cons object" } { "quot" "a quotation with stack effect ( -- X )" } { "result" "resulting cons object" } } { $values { "list" "a cons object" } { "quot" "a quotation with stack effect ( -- X )" } { "result" "resulting cons object" } }
{ $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." } { $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* } ; { $see-also leach lmap lmap-with ltake lappend lfrom lfrom-by lconcat lcartesian-product lcartesian-product* lcomp lcomp* } ;
HELP: list>vector HELP: list>vector
{ $values { "list" "a cons object" } { "vector" "the list converted to a vector" } } { $values { "list" "a cons object" } { "vector" "the list converted to a vector" } }
@ -126,17 +126,17 @@ HELP: list>array
HELP: lappend HELP: lappend
{ $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." }
{ $see-also leach lmap lmap-with ltake lsubset lfrom lfrom-by lconcat lcartesian-product lcartesian-product* } ; { $see-also leach lmap lmap-with ltake lsubset lfrom lfrom-by lconcat lcartesian-product lcartesian-product* lcomp lcomp* } ;
HELP: lfrom-by HELP: lfrom-by
{ $values { "n" "an integer" } { "quot" "a quotation with stack effect ( -- int )" } { "result" "a lazy list of integers" } } { $values { "n" "an integer" } { "quot" "a quotation with stack effect ( -- int )" } { "result" "a lazy list of integers" } }
{ $description "Return an infinite lazy list of values starting from n, with each successive value being the result of applying quot to n." } { $description "Return an infinite lazy list of values starting from n, with each successive value being the result of applying quot to n." }
{ $see-also leach lmap lmap-with ltake lsubset lfrom lconcat lcartesian-product lcartesian-product* } ; { $see-also leach lmap lmap-with ltake lsubset lfrom lconcat lcartesian-product lcartesian-product* lcomp lcomp* } ;
HELP: lfrom HELP: lfrom
{ $values { "n" "an integer" } { "result" "a lazy list of integers" } } { $values { "n" "an integer" } { "result" "a lazy list of integers" } }
{ $description "Return an infinite lazy list of incrementing integers starting from n." } { $description "Return an infinite lazy list of incrementing integers starting from n." }
{ $see-also leach lmap lmap-with ltake lsubset lfrom-by lconcat lcartesian-product lcartesian-product* } ; { $see-also leach lmap lmap-with ltake lsubset lfrom-by lconcat lcartesian-product lcartesian-product* lcomp lcomp* } ;
HELP: seq>list HELP: seq>list
{ $values { "seq" "a sequence" } { "list" "a list" } } { $values { "seq" "a sequence" } { "list" "a list" } }
@ -146,24 +146,28 @@ HELP: seq>list
HELP: lconcat HELP: lconcat
{ $values { "list" "a list of lists" } { "result" "a list" } } { $values { "list" "a list of lists" } { "result" "a list" } }
{ $description "Concatenates a list of lists together into one list." } { $description "Concatenates a list of lists together into one list." }
{ $see-also leach lmap lmap-with ltake lsubset lcartesian-product lcartesian-product* lfrom-by } ; { $see-also leach lmap lmap-with ltake lsubset lcartesian-product lcartesian-product* lfrom-by lcomp lcomp* } ;
HELP: lcartesian-product HELP: lcartesian-product
{ $values { "list1" "a list" } { "list2" "a list" } { "result" "list of cartesian products" } } { $values { "list1" "a list" } { "list2" "a list" } { "result" "list of cartesian products" } }
{ $description "Given two lists, return a list containing the cartesian product of those lists." } { $description "Given two lists, return a list containing the cartesian product of those lists." }
{ $see-also leach lmap lmap-with lconcat ltake lsubset lfrom-by lcartesian-product* } ; { $see-also leach lmap lmap-with lconcat ltake lsubset lfrom-by lcartesian-product* lcomp lcomp* } ;
HELP: lcartesian-product* HELP: lcartesian-product*
{ $values { "list" "a list of lists" } { "result" "list of cartesian products" } } { $values { "list" "a list of lists" } { "result" "list of cartesian products" } }
{ $description "Given a list of lists, return a list containing the cartesian product of those lists." } { $description "Given a list of lists, return a list containing the cartesian product of those lists." }
{ $see-also leach lmap lmap-with lconcat ltake lsubset lfrom-by lcartesian-product } ; { $see-also leach lmap lmap-with lconcat ltake lsubset lfrom-by lcartesian-product lcomp lcomp* } ;
HELP: lcomp2 HELP: lcomp
{ $values { "list1" "a list" } { "list2" "a list" } { "quot" "a quotation with stack effect ( A B -- C )" } { "list" "the resulting list" } } { $values { "list" "a list of lists" } { "quot" "a quotation with stack effect ( seq -- X )" } { "list" "the resulting list" } }
{ $description "List comprehension for the case where there are two input lists. The cartesian product of the two lists is generated and 'quot' called with each element from the cartesian product on the stack, the result of which is returned in the final 'list'." } { $description "Get the cartesian product of the lists in 'list' and call 'quot' call with each element from the cartesian product on the stack, the result of which is returned in the final 'list'." }
{ $see-also leach lmap lmap-with lconcat ltake lsubset lfrom-by lcartesian-product lcomp3 } ; { $see-also leach lmap lmap-with lconcat ltake lsubset lfrom-by lcartesian-product lcomp* } ;
HELP: lcomp*
{ $values { "list" "a list of lists" } { "guards" "a sequence of quotations with stack effect ( seq -- bool )" } { "quot" "a quotation with stack effect ( seq -- X )" } { "list" "the resulting list" } }
{ $description "Get the cartesian product of the lists in 'list', filter it by applying each guard quotation to it and call 'quot' call with each element from the remaining cartesian product items on the stack, the result of which is returned in the final 'list'." }
{ $examples
{ $example "{ 1 2 3 } seq>list { 4 5 6 } seq>list 2list { [ first odd? ] } [ + ] lcomp*" }
}
{ $see-also leach lmap lmap-with lconcat ltake lsubset lfrom-by lcartesian-product lcomp } ;
HELP: lcomp3
{ $values { "list1" "a list" } { "list2" "a list" } { "list3" "a list" } { "quot" "a quotation with stack effect ( A B C -- D )" } { "list" "the resulting list" } }
{ $description "List comprehension for the case where there are three input lists. The cartesian product of the lists are generated and 'quot' called with each element from the cartesian product on the stack, the result of which is returned in the final 'list'." }
{ $see-also leach lmap lmap-with lconcat ltake lsubset lfrom-by lcartesian-product lcomp2 } ;