]> gitweb.factorcode.org Git - factor.git/commitdiff
nmap-as and nmap combinators
authorJoe Groff <arcata@gmail.com>
Wed, 14 Oct 2009 05:38:51 +0000 (00:38 -0500)
committerJoe Groff <arcata@gmail.com>
Wed, 14 Oct 2009 05:38:51 +0000 (00:38 -0500)
basis/generalizations/generalizations-docs.factor
basis/generalizations/generalizations-tests.factor
basis/generalizations/generalizations.factor

index cc559ca76ebfc15c6e82f47fa8f20e5c7b1471a2..5df2008e74609b12bae8407b09ccd8ab1e3c3cc0 100644 (file)
@@ -237,9 +237,17 @@ HELP: nspread
 } ;\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
@@ -350,6 +358,8 @@ ARTICLE: "combinator-generalizations" "Generalized combinators"
     ncleave\r
     nspread\r
     neach\r
+    nmap\r
+    nmap-as\r
 } ;\r
 \r
 ARTICLE: "other-generalizations" "Additional generalizations"\r
index 2c957fefdf673343cbd72967e23617302d927272..3ae504a33d2eda7f9efde026e2c6a8cb5461001b 100644 (file)
@@ -82,6 +82,10 @@ IN: generalizations.tests
 \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
@@ -94,3 +98,21 @@ D4d$
     { "!" "@" "#" "$" }\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
index 38f8e29da3d993641740e508a0a5b34a9723afbc..d98cc0afa383f4303096538663e7e3a2b8566635 100644 (file)
@@ -128,5 +128,11 @@ MACRO: (neach) ( n -- )
     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