sequences.generalizations: adding nmap-reduce.

db4
John Benediktsson 2012-09-26 20:48:50 -07:00
parent 13db482b7f
commit 4dd3511a40
3 changed files with 17 additions and 0 deletions

View File

@ -110,6 +110,10 @@ HELP: nproduce-as
{ $values { "pred" { $quotation "( -- ? )" } } { "quot" { $quotation "( -- obj1 obj2 ... objn )" } } { "exemplar..." { $snippet "n" } " sequences on the datastack" } { "n" integer } { "seq..." { $snippet "n" } " sequences on the datastack of the same types as the " { $snippet "exemplar" } "s" } }
{ $description "A generalization of " { $link produce-as } " that generates " { $snippet "n" } " sequences in parallel by calling " { $snippet "quot" } " repeatedly until " { $snippet "pred" } " outputs false." } ;
HELP: nmap-reduce
{ $values { "map-quot" quotation } { "reduce-quot" quotation } { "n" integer } }
{ $description "A generalization of " { $link map-reduce } " that can be applied to any number of sequences." } ;
ARTICLE: "sequences.generalizations" "Generalized sequence words"
"The " { $vocab-link "sequences.generalizations" } " vocabulary defines generalized versions of various sequence operations."
{ $subsections

View File

@ -132,3 +132,7 @@ D4d$
[ { 10 8 6 4 2 } { 9 7 5 3 1 } ]
[ 10 nproduce-test ] unit-test
{ 45 } [
{ 1 2 3 } { 4 5 6 } { 7 8 9 } [ + + ] [ + ] 3 nmap-reduce
] unit-test

View File

@ -42,6 +42,9 @@ MACRO: nmin-length ( n -- )
dup 1 - [ min ] n*quot
'[ [ length ] _ napply @ ] ;
: nnth ( n seq... n -- )
[ nth ] swap [ apply-curry ] [ cleave* ] bi ; inline
: nnth-unsafe ( n seq... n -- )
[ nth-unsafe ] swap [ apply-curry ] [ cleave* ] bi ; inline
@ -112,3 +115,9 @@ MACRO: (ncollect) ( n -- )
: nproduce ( pred quot n -- seq... )
[ { } swap dupn ] keep nproduce-as ; inline
MACRO: nmap-reduce ( map-quot reduce-quot n -- quot )
-rot dupd compose [ over ] dip over '[
[ [ first ] _ napply @ 1 ] _ nkeep
_ _ (neach) (each-integer)
] ;