]> gitweb.factorcode.org Git - factor.git/commitdiff
assocs: moving map-index back to sequences, change zip and zip-index to make alists.
authorJohn Benediktsson <mrjbq7@gmail.com>
Wed, 12 Nov 2014 02:13:01 +0000 (18:13 -0800)
committerJohn Benediktsson <mrjbq7@gmail.com>
Wed, 12 Nov 2014 02:13:01 +0000 (18:13 -0800)
core/assocs/assocs-docs.factor
core/assocs/assocs-tests.factor
core/assocs/assocs.factor
core/sequences/sequences-docs.factor
core/sequences/sequences.factor

index f03d04bca39660b3105f59d059b2cbe45d09d783..779248f856515598fa819a2be7a212c94f8b2986 100644 (file)
@@ -517,7 +517,7 @@ HELP: unzip
 HELP: zip
 { $values
      { "keys" sequence } { "values" sequence }
-     { "assoc" assoc } }
+     { "alist" "an array of key/value pairs" } }
 { $description "Combines two sequences pairwise into a single sequence of key/value pairs." }
 { $examples
     { $example "USING: prettyprint assocs ;"
@@ -525,7 +525,6 @@ HELP: zip
                "{ { 1 4 } { 2 5 } { 3 6 } }"
     }
 } ;
-{ unzip zip } related-words
 
 HELP: zip-as
 { $values
@@ -543,7 +542,7 @@ HELP: zip-as
 HELP: zip-index
 { $values
     { "values" sequence }
-    { "assoc" assoc }
+    { "alist" "an array of key/value pairs" }
 }
 { $examples
     "Zip a sequnce with its indices:"
@@ -568,24 +567,4 @@ HELP: zip-index-as
 }
 { $description "Zip a sequence with its index and return an associative list of type " { $snippet "exemplar" } " where the input sequence is the keys and the indices are the values." } ;
 
-HELP: map-index
-{ $values
-  { "seq" sequence } { "quot" { $quotation ( ... elt index -- ... newelt ) } } { "newseq" sequence } }
-{ $description "Calls the quotation with the element of the sequence and its index on the stack, with the index on the top of the stack. Collects the outputs of the quotation and outputs them in a sequence of the same type as the input sequence." }
-{ $examples { $example "USING: arrays assocs prettyprint ;"
-"{ 10 20 30 } [ 2array ] map-index ."
-"{ { 10 0 } { 20 1 } { 30 2 } }"
-} } ;
-
-HELP: map-index-as
-{ $values
-  { "seq" sequence } { "quot" { $quotation ( ... elt index -- ... newelt ) } } { "exemplar" sequence } { "obj" object } }
-{ $description "Calls the quotation with the element of the sequence and its index on the stack, with the index on the top of the stack. Collects the outputs of the quotation and outputs them in a sequence of the same type as the " { $snippet "exemplar" } "." }
-{ $examples { $example "USING: arrays assocs prettyprint ;"
-"{ 10 20 30 } [ 2array ] V{ } map-index-as ."
-"V{ { 10 0 } { 20 1 } { 30 2 } }"
-} } ;
-{ map-index map-index-as } related-words
-
-
 { unzip zip zip-as zip-index zip-index-as } related-words
index dc036799b4c0bb2a02624613928364b272e05490..86890058d750644669245bd6c91391da3bba2737 100644 (file)
@@ -213,34 +213,13 @@ unit-test
     { { 1 f } { f 2 } } sift-values
 ] unit-test
 
-! map-index, map-index-as
-{
-    { 11 23 35 }
-} [ { 11 22 33 } [ + ] map-index ] unit-test
-
-{
-    V{ 11 23 35 }
-} [ V{ 11 22 33 } [ + ] map-index ] unit-test
-
-{
-    V{ 11 23 35 }
-} [ { 11 22 33 } [ + ] V{ } map-index-as ] unit-test
-
-{
-    B{ 11 23 35 }
-} [ { 11 22 33 } [ + ] B{ } map-index-as ] unit-test
-
-{
-    BV{ 11 23 35 }
-} [ { 11 22 33 } [ + ] BV{ } map-index-as ] unit-test
-
 ! zip, zip-as
 {
     { { 1 4 } { 2 5 } { 3 6 } }
 } [ { 1 2 3 } { 4 5 6 } zip ] unit-test
 
 {
-    V{ { 1 4 } { 2 5 } { 3 6 } }
+    { { 1 4 } { 2 5 } { 3 6 } }
 } [ V{ 1 2 3 } { 4 5 6 } zip ] unit-test
 
 {
@@ -276,7 +255,7 @@ unit-test
 } [ { 11 22 33 } zip-index ] unit-test
 
 {
-    V{ { 11 0 } { 22 1 } { 33 2 } }
+    { { 11 0 } { 22 1 } { 33 2 } }
 } [ V{ 11 22 33 } zip-index ] unit-test
 
 {
index ce532a36a7ceaf6910c23329a7dc27210b7dbd94..786bd64f02e5d2f52ac46690d8f3c4d3551aac7d 100644 (file)
@@ -206,26 +206,14 @@ M: assoc value-at* swap [ = nip ] curry assoc-find nip ;
         [ [ set-at ] with-assoc 2each ] keep
     ] if ; inline
 
- : zip ( keys values -- assoc )
-     over zip-as ; inline
-
-: map-index-as ( ... seq quot: ( ... elt index -- ... newelt ) exemplar -- ... obj )
-    dup sequence? [
-        [ dup length iota ] 2dip 2map-as
-    ] [
-        [ dup length iota ] 2dip [ over length ] dip new-assoc
-        ! Need to do 2array/first2 here because of quot's stack effect
-        [ [ [ first2 swap ] dip set-at ] curry compose 2each ] keep
-    ] if ; inline
-
-: map-index ( ... seq quot: ( ... elt index -- ... newelt ) -- ... newseq )
-    over map-index-as ; inline
+: zip ( keys values -- alist )
+     { } zip-as ; inline
 
 : zip-index-as ( values exemplar -- assoc )
-    [ [ 2array ] ] dip map-index-as ; inline
+    [ dup length iota ] dip zip-as ; inline
 
-: zip-index ( values -- assoc )
-    dup zip-index-as ; inline
+: zip-index ( values -- alist )
+    { } zip-index-as ; inline
 
 : unzip ( assoc -- keys values )
     dup assoc-empty? [ drop { } { } ] [ >alist flip first2 ] if ;
index 1bb69f422a65f4707ffa4ffe23a879e46e55def0..3caf128de2d276dd53ef6b64f966ff93c89c5d2d 100644 (file)
@@ -365,6 +365,30 @@ HELP: each-index
 "{ 10 0 }\n{ 20 1 }\n{ 30 2 }"
 } } ;
 
+HELP: map-index
+{ $values
+  { "seq" sequence } { "quot" { $quotation ( ... elt index -- ... newelt ) } } { "newseq" sequence } }
+{ $description "Calls the quotation with the element of the sequence and its index on the stack, with the index on the top of the stack. Collects the outputs of the quotation and outputs them in a sequence of the same type as the input sequence." }
+{ $examples
+    { $example "USING: arrays sequences prettyprint ;"
+        "{ 10 20 30 } [ 2array ] map-index ."
+        "{ { 10 0 } { 20 1 } { 30 2 } }"
+    }
+} ;
+
+HELP: map-index-as
+{ $values
+  { "seq" sequence } { "quot" { $quotation ( ... elt index -- ... newelt ) } } { "exemplar" sequence } { "newseq" sequence } }
+{ $description "Calls the quotation with the element of the sequence and its index on the stack, with the index on the top of the stack. Collects the outputs of the quotation and outputs them in a sequence of the same type as the " { $snippet "exemplar" } " sequence." }
+{ $examples
+    { $example "USING: arrays sequences prettyprint ;"
+        "{ 10 20 30 } [ 2array ] V{ } map-index-as ."
+        "V{ { 10 0 } { 20 1 } { 30 2 } }"
+    }
+} ;
+
+{ map-index map-index-as } related-words
+
 HELP: change-nth
 { $values { "i" "a non-negative integer" } { "seq" "a mutable sequence" } { "quot" { $quotation ( ..a elt -- ..b newelt ) } } }
 { $description "Applies the quotation to the " { $snippet "i" } "th element of the sequence, storing the result back into the sequence." }
index 8eaa62c43b514d2928647682fa815c459fdf4b99..a17d645013865b5d0c1cfd735b1db055a029df34 100644 (file)
@@ -571,6 +571,12 @@ PRIVATE>
 : each-index ( ... seq quot: ( ... elt index -- ... ) -- ... )
     (each-index) each-integer ; inline
 
+: map-index-as ( ... seq quot: ( ... elt index -- ... newelt ) exemplar -- ... newseq )
+    [ dup length iota ] 2dip 2map-as ; inline
+
+: map-index ( ... seq quot: ( ... elt index -- ... newelt ) -- ... newseq )
+    { } map-index-as ; inline
+
 : interleave ( ... seq between quot: ( ... elt -- ... ) -- ... )
     pick empty? [ 3drop ] [
         [ [ drop first-unsafe ] dip call ]