]> gitweb.factorcode.org Git - factor.git/commitdiff
combinators.smart: adding smart-2reduce and smart-2map-reduce, for @erg.
authorJohn Benediktsson <mrjbq7@gmail.com>
Thu, 27 Sep 2012 00:42:59 +0000 (17:42 -0700)
committerJohn Benediktsson <mrjbq7@gmail.com>
Thu, 27 Sep 2012 00:43:26 +0000 (17:43 -0700)
basis/combinators/smart/smart-docs.factor
basis/combinators/smart/smart-tests.factor
basis/combinators/smart/smart.factor

index 2f1b21f92ccf8a469d9ec05e92b15adaff74ce35..cab79ad675c9076bcbabe942634f7e8dbfee9aaf 100644 (file)
@@ -239,6 +239,14 @@ HELP: smart-map-reduce
 { $values { "map-reduce-quots" sequence } }
 { $description "A version of " { $link map-reduce } " that takes a sequence of " { $snippet "{ map-quot reduce-quot }" } " pairs, returning the " { $link map-reduce } " result for each pair." } ;
 
+HELP: smart-2reduce
+{ $values { "2reduce-quots" sequence } }
+{ $description "A version of " { $link 2reduce } " that takes a sequence of " { $snippet "{ identity 2reduce-quot }" } " pairs, returning the " { $link 2reduce } " result for each pair." } ;
+
+HELP: smart-2map-reduce
+{ $values { "2map-reduce-quots" sequence } }
+{ $description "A version of " { $link 2map-reduce } " that takes a sequence of " { $snippet "{ 2map-quot 2reduce-quot }" } " pairs, returning the " { $link 2map-reduce } " result for each pair." } ;
+
 ARTICLE: "combinators.smart" "Smart combinators"
 "A " { $emphasis "smart combinator" } " is a macro which reflects on the stack effect of an input quotation. The " { $vocab-link "combinators.smart" } " vocabulary implements a few simple smart combinators which look at the static stack effects of input quotations and generate code which produces or consumes the relevant number of stack values." $nl
 "Take all input values from a sequence:"
index 95699524336fff39fec2fc5ce4f3dfb173ae7f7d..dd803bb2a07881ece08c441f900c5b114f4a1fe3 100644 (file)
@@ -109,3 +109,17 @@ IN: combinators.smart.tests
         { [ ] [ + ] }
     } smart-map-reduce
 ] unit-test
+
+{ 0 12 } [
+    { 1 2 3 } dup {
+        { 0 [ - + ] }
+        { 0 [ + + ] }
+    } smart-2reduce
+] unit-test
+
+{ 36 12 } [
+    { 1 2 3 } dup {
+        { [ * ] [ * ] }
+        { [ + ] [ + ] }
+    } smart-2map-reduce
+] unit-test
index 612701e52e73b9c404cfc95eb454078ee8da1e6e..493b39a9ebcb78d7638f6eb2b7b75204b1cea655 100644 (file)
@@ -141,3 +141,16 @@ MACRO: smart-map-reduce ( map-reduce-quots -- quot )
         [ @ _ cleave-curry _ spread* ]
         [ 1 ] 2dip (each) (each-integer)
     ] ;
+
+MACRO: smart-2reduce ( 2reduce-quots -- quot )
+    unzip [ [ ] like ] bi@ dup length dup '[
+        [ @ ] 2dip
+        [ @ _ [ cleave-curry ] [ cleave-curry ] bi _ spread* ] 2each
+    ] ;
+
+MACRO: smart-2map-reduce ( 2map-reduce-quots -- quot )
+    [ keys ] [ [ [ ] concat-as ] [ ] map-as ] bi dup length dup '[
+        [ [ first ] bi@ _ 2cleave ] 2keep
+        [ @ _ [ cleave-curry ] [ cleave-curry ] bi _ spread* ]
+        [ 1 ] 3dip (2each) (each-integer)
+    ] ;