]> gitweb.factorcode.org Git - factor.git/commitdiff
Implement lreduce in lazy-lists
authorSamuel Tardieu <sam@rfc1149.net>
Thu, 27 Dec 2007 14:34:15 +0000 (15:34 +0100)
committerSamuel Tardieu <sam@rfc1149.net>
Thu, 27 Dec 2007 15:01:28 +0000 (16:01 +0100)
extra/lazy-lists/lazy-lists-docs.factor
extra/lazy-lists/lazy-lists.factor

index f539e358354648ac2891bb42eac00a7a6fba968e..11afc9b6b5a87dd83de5be7da1bac9395281a60d 100644 (file)
@@ -82,12 +82,16 @@ HELP: uncons
 { $values { "cons" "a cons object" } { "car" "the head of the list" } { "cdr" "the tail of the list" } }
 { $description "Put the head and tail of the list on the stack." } ;
 
-{ leach lmap lmap-with ltake lsubset lappend lfrom lfrom-by lconcat lcartesian-product lcartesian-product* lcomp lcomp* lmerge lreduce lwhile luntil } related-words
+{ leach lreduce lmap lmap-with ltake lsubset lappend lfrom lfrom-by lconcat lcartesian-product lcartesian-product* lcomp lcomp* lmerge lreduce lwhile luntil } related-words
 
 HELP: leach
 { $values { "list" "a cons object" } { "quot" "a quotation with stack effect ( obj -- )" } }
 { $description "Call the quotation for each item in the list." } ;
 
+HELP: lreduce
+{ $values { "list" "a cons object" } { "identity" "an object" } { "quot" "a quotation with stack effect ( prev elt -- next )" } { "result" "the final result" } }
+{ $description "Combines successive elements of the list using a binary operation, and outputs the final result." } ;
+
 HELP: lmap
 { $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." } ;
index c42daabc0508521f68b9771830a70c0305fa2751..9f2e05c7ba408c33158fdb3b6a813f67a8b49547 100644 (file)
@@ -102,6 +102,9 @@ M: lazy-cons list? ( object -- bool )
 : leach ( list quot -- )
   swap dup nil? [ 2drop ] [ uncons swapd over 2slip leach ] if ; inline
 
+: lreduce ( list identity quot -- result )
+  swapd leach ; inline
+
 TUPLE: memoized-cons original car cdr nil? ;
 
 : not-memoized ( -- obj )