combinators.smart: adding smart-2reduce and smart-2map-reduce, for @erg.

db4
John Benediktsson 2012-09-26 17:42:59 -07:00
parent d751c87283
commit 5903b2ccf5
3 changed files with 35 additions and 0 deletions

View File

@ -239,6 +239,14 @@ HELP: smart-map-reduce
{ $values { "map-reduce-quots" sequence } } { $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." } ; { $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" 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 "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:" "Take all input values from a sequence:"

View File

@ -109,3 +109,17 @@ IN: combinators.smart.tests
{ [ ] [ + ] } { [ ] [ + ] }
} smart-map-reduce } smart-map-reduce
] unit-test ] 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

View File

@ -141,3 +141,16 @@ MACRO: smart-map-reduce ( map-reduce-quots -- quot )
[ @ _ cleave-curry _ spread* ] [ @ _ cleave-curry _ spread* ]
[ 1 ] 2dip (each) (each-integer) [ 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)
] ;