} ;\r
\r
HELP: neach\r
-{ $values { "n" integer } }\r
+{ $values { "...seq" "a set of " { $snippet "n" } " sequences" } { "quot" "a quotation with stack effect " { $snippet "( ...element -- )" } } { "n" integer } }\r
{ $description "A generalization of " { $link each } ", " { $link 2each } ", and " { $link 3each } " that can iterate over any number of sequences in parallel." } ;\r
\r
+HELP: nmap\r
+{ $values { "...seq" "a set of " { $snippet "n" } " sequences" } { "quot" "a quotation with stack effect " { $snippet "( ...element -- result )" } } { "n" integer } { "result" "a sequence of the same type as the first " { $snippet "seq" } } }\r
+{ $description "A generalization of " { $link map } ", " { $link 2map } ", and " { $link 3map } " that can map over any number of sequences in parallel." } ;\r
+\r
+HELP: nmap-as\r
+{ $values { "...seq" "a set of " { $snippet "n" } " sequences" } { "quot" "a quotation with stack effect " { $snippet "( ...element -- result )" } } { "exemplar" sequence } { "n" integer } { "result" "a sequence of the same type as " { $snippet "exemplar" } } }\r
+{ $description "A generalization of " { $link map-as } ", " { $link 2map-as } ", and " { $link 3map-as } " that can map over any number of sequences in parallel." } ;\r
+\r
HELP: mnswap\r
{ $values { "m" integer } { "n" integer } }\r
{ $description "Swaps the top " { $snippet "m" } " stack elements with the " { $snippet "n" } " elements directly underneath." }\r
ncleave\r
nspread\r
neach\r
+ nmap\r
+ nmap-as\r
} ;\r
\r
ARTICLE: "other-generalizations" "Additional generalizations"\r
\r
: neach-test ( a b c d -- )\r
[ 4 nappend print ] 4 neach ;\r
+: nmap-test ( a b c d -- e )\r
+ [ 4 nappend ] 4 nmap ;\r
+: nmap-as-test ( a b c d -- e )\r
+ [ 4 nappend ] [ ] 4 nmap-as ;\r
\r
[ """A1a!\r
B2b@\r
{ "!" "@" "#" "$" }\r
[ neach-test ] with-string-writer\r
] unit-test\r
+\r
+[ { "A1a!" "B2b@" "C3c#" "D4d$" } ]\r
+[ \r
+ { "A" "B" "C" "D" }\r
+ { "1" "2" "3" "4" }\r
+ { "a" "b" "c" "d" }\r
+ { "!" "@" "#" "$" }\r
+ nmap-test\r
+] unit-test\r
+\r
+[ [ "A1a!" "B2b@" "C3c#" "D4d$" ] ]\r
+[ \r
+ { "A" "B" "C" "D" }\r
+ { "1" "2" "3" "4" }\r
+ { "a" "b" "c" "d" }\r
+ { "!" "@" "#" "$" }\r
+ nmap-as-test\r
+] unit-test\r
dup dup dup
'[ [ [ _ nmin-length ] _ nkeep [ _ nnth-unsafe ] _ ncurry ] dip compose ] ;
-: neach ( ... seq quot n -- )
+: neach ( ...seq quot n -- )
(neach) each-integer ; inline
+
+: nmap-as ( ...seq quot exemplar n -- result )
+ '[ _ (neach) ] dip map-integers ; inline
+
+: nmap ( ...seq quot n -- result )
+ dup '[ [ _ npick ] dip swap ] dip nmap-as ; inline