]> gitweb.factorcode.org Git - factor.git/commitdiff
sequences.deep: adding deep-reduce.
authorJohn Benediktsson <mrjbq7@gmail.com>
Tue, 9 Jun 2015 15:42:54 +0000 (08:42 -0700)
committerJohn Benediktsson <mrjbq7@gmail.com>
Tue, 9 Jun 2015 15:42:54 +0000 (08:42 -0700)
basis/sequences/deep/deep-docs.factor
basis/sequences/deep/deep.factor

index d2661d6d41451554e321ade508aac82f4e4c9a1d..af33e101799a22c719906afa83fcec407722ea9d 100644 (file)
@@ -6,6 +6,11 @@ HELP: deep-each
 { $description "Execute a quotation on each nested element of an object and its children, in preorder." }
 { $see-also each } ;
 
+HELP: deep-reduce
+{ $values { "obj" object } { "identity" object } { "quot" { $quotation ( ... prev elt -- ... next ) } } { "result" "the final result" } }
+{ $description "Execute a quotation on each nested element of an object and its children, in preorder. The first iteration is called with " { $code "identity" } " and the first element. Subsequence iterations are called with the result of the previous iteration and the next element." }
+{ $see-also reduce } ;
+
 HELP: deep-map
 { $values { "obj" object } { "quot" { $quotation ( ... elt -- ... elt' ) } } { "newobj" "the mapped object" } }
 { $description "Execute a quotation on each nested element of an object and its children, in preorder. That is, the result of the execution of the quotation on the outer is used to map the inner elements." }
index d748263b02f0e3395bc6716f429c59a3152e8bd7..86388a8af0d208da0932faac266dd5fb5b7ecccf 100644 (file)
@@ -16,6 +16,9 @@ M: object branch? drop f ;
     [ call ] 2keep over branch?
     [ '[ _ deep-each ] each ] [ 2drop ] if ; inline recursive
 
+: deep-reduce ( ... obj identity quot: ( ... prev elt -- ... next ) -- ... result )
+    swapd deep-each ; inline
+
 : deep-map ( ... obj quot: ( ... elt -- ... elt' ) -- ... newobj )
     [ call ] keep over branch?
     [ '[ _ deep-map ] map ] [ drop ] if ; inline recursive