Permalink
Browse files

sequences.generalizations: adding nmap-reduce.

  • Loading branch information...
1 parent 29c1424 commit 23c6d2f1d66fa445ab06222911698fca300b0385 @mrjbq7 mrjbq7 committed Sep 27, 2012
@@ -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
@@ -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
@@ -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)
+ ] ;

0 comments on commit 23c6d2f

Please sign in to comment.