]> gitweb.factorcode.org Git - factor.git/commitdiff
Move firstn to generalizations, clean it up a bit
authorSlava Pestov <slava@slava-pestovs-macbook-pro.local>
Mon, 14 Jul 2008 00:50:37 +0000 (19:50 -0500)
committerSlava Pestov <slava@slava-pestovs-macbook-pro.local>
Mon, 14 Jul 2008 00:50:37 +0000 (19:50 -0500)
extra/generalizations/generalizations-docs.factor
extra/generalizations/generalizations-tests.factor
extra/generalizations/generalizations.factor
extra/geo-ip/geo-ip.factor
extra/namespaces/lib/lib.factor
extra/sequences/lib/lib-tests.factor
extra/sequences/lib/lib.factor
extra/usa-cities/usa-cities.factor

index d2af13a9c3393bc43bf7cd880a62bdc39a4aa8e9..a702f452da6d8764c29d8f499e66b83336b65829 100755 (executable)
@@ -1,9 +1,21 @@
-! Copyright (C) 2007 Chris Double.\r
-! See http://factorcode.org/license.txt for BSD license.\r
 USING: help.syntax help.markup kernel sequences quotations\r
-math ;\r
+math arrays ;\r
 IN: generalizations\r
 \r
+HELP: narray\r
+{ $values { "n" integer } }\r
+{ $description "A generalization of " { $link 1array } ", "\r
+{ $link 2array } ", " { $link 3array } " and " { $link 4array } " "\r
+"that constructs an array from the top " { $snippet "n" } " elements of the stack."\r
+} ;\r
+\r
+HELP: firstn\r
+{ $values { "n" integer } }\r
+{ $description "A generalization of " { $link first } ", "\r
+{ $link first2 } ", " { $link first3 } " and " { $link first4 } " "\r
+"that pushes the first " { $snippet "n" } " elements of a sequence on the stack."\r
+} ;\r
+\r
 HELP: npick\r
 { $values { "n" integer } }\r
 { $description "A generalization of " { $link dup } ", "\r
@@ -119,6 +131,7 @@ ARTICLE: "generalizations" "Generalized shuffle words and combinators"
 "macros where the arity of the input quotations depends on an "\r
 "input parameter."\r
 { $subsection narray }\r
+{ $subsection firstn }\r
 { $subsection ndup }\r
 { $subsection npick }\r
 { $subsection nrot }\r
index af010e202682e6418612a211d2e0eb565b788d2c..75985c936892c74b92bcd009eab187057134811a 100755 (executable)
@@ -32,3 +32,7 @@ IN: generalizations.tests
 [ [ dup 2^ 2array ] 5 napply ] must-infer\r
 \r
 [ { "xyc" "xyd" } ] [ "x" "y" { "c" "d" } [ 3append ] 2 nwith map ] unit-test\r
+\r
+[ 1 2 3 4 ] [ { 1 2 3 4 } 4 firstn ] unit-test\r
+[ ] [ { } 0 firstn ] unit-test\r
+[ "a" ] [ { "a" } 1 firstn ] unit-test\r
index 6cbb13518e05d25b027f29a6b246531bb9b420aa..99fa8795ae26c509110823206f222f02be6a6a29 100755 (executable)
@@ -1,14 +1,20 @@
-! Copyright (C) 2007, 2008 Chris Double, Doug Coleman.\r
+! Copyright (C) 2007, 2008 Chris Double, Doug Coleman, Eduardo\r
+! Cavazos, Slava Pestov.\r
 ! See http://factorcode.org/license.txt for BSD license.\r
 USING: kernel sequences sequences.private namespaces math math.ranges\r
 combinators macros quotations fry locals arrays ;\r
 IN: generalizations\r
 \r
 MACRO: narray ( n -- quot )\r
-    dup [ f <array> ] curry\r
-    swap <reversed> [\r
-        [ swap [ set-nth-unsafe ] keep ] curry\r
-    ] map concat append ;\r
+    [ <reversed> ] [ '[ , f <array> ] ] bi\r
+    [ '[ @ [ , swap set-nth-unsafe ] keep ] ] reduce ;\r
+\r
+MACRO: firstn ( n -- )\r
+    dup zero? [ drop [ drop ] ] [\r
+        [ [ '[ , _ nth-unsafe ] ] map ]\r
+        [ 1- '[ , _ bounds-check 2drop ] ]\r
+        bi prefix '[ , cleave ]\r
+    ] if ;\r
 \r
 MACRO: npick ( n -- )\r
     1- dup saver [ dup ] rot [ r> swap ] n*quot 3append ;\r
index 62cc65939440bdb17ba0016f6f22799c314773fb..aee53f24f50e1dda34747e1af1e6347b381f232d 100644 (file)
@@ -1,5 +1,7 @@
+! Copyright (C) 2008 Slava Pestov.
+! See http://factorcode.org/license.txt for BSD license.
 USING: kernel sequences io.files io.launcher io.encodings.ascii
-io.streams.string http.client sequences.lib combinators
+io.streams.string http.client generalizations combinators
 math.parser math.vectors math.intervals interval-maps memoize
 csv accessors assocs strings math splitting grouping arrays ;
 IN: geo-ip
index 9ad8978bf34e26099b84f23360c12a3c0c06e79c..4da3935727ec56f04ec612c71715a9f24686ee36 100755 (executable)
@@ -2,7 +2,7 @@
 ! USING: kernel quotations namespaces sequences assocs.lib ;
 
 USING: kernel namespaces namespaces.private quotations sequences
-       assocs.lib math.parser math sequences.lib locals mirrors ;
+       assocs.lib math.parser math generalizations locals mirrors ;
 
 IN: namespaces.lib
 
index 4b8114f67fab08e2a60504b1d2529f4b20600445..3744a7217a6a3a1cb5a1aa7330186fab03e4f4b5 100755 (executable)
@@ -67,11 +67,6 @@ IN: sequences.lib.tests
 { 13 } [ 1 2 { 3 4 } [ + + ] 2 each-withn + ] unit-test
 [ { 910 911 912 } ] [ 10 900 3 [ + + ] map-with2 ] unit-test
 
-[ 1 2 3 4 ] [ { 1 2 3 4 } 4 firstn ] unit-test
-
-[ ] [ { } 0 firstn ] unit-test
-[ "a" ] [ { "a" } 1 firstn ] unit-test
-
 [ "empty" ] [ { } [ "not empty" ] [ "empty" ] if-seq ] unit-test
 [ { 1 } "not empty" ] [ { 1 } [ "not empty" ] [ "empty" ] if-seq ] unit-test
 
index 0049320b94c453d050499267a8122333ccda6923..9f8e5be3d5ce9e6d7ec915a4c9603370d15d3cf0 100755 (executable)
@@ -20,11 +20,6 @@ IN: sequences.lib
 
 : map-with2 ( obj obj list quot -- newseq ) 2 map-withn ; inline
 
-MACRO: firstn ( n -- )
-    [ [ swap nth ] curry [ keep ] curry ] map
-    concat >quotation
-    [ drop ] compose ;
-
 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
 
 : each-percent ( seq quot -- )
index fb392542f3dd160d303c76c1df47620c73126a3e..968bf9d053fd636ef255d1815bf2bcb39b92bb15 100644 (file)
@@ -1,6 +1,6 @@
 ! Copyright (C) 2008 Slava Pestov.
 ! See http://factorcode.org/license.txt for BSD license.
-USING: io.files io.encodings.ascii sequences sequences.lib
+USING: io.files io.encodings.ascii sequences generalizations
 math.parser combinators kernel memoize csv symbols summary
 words accessors math.order sorting ;
 IN: usa-cities