]> gitweb.factorcode.org Git - factor.git/commitdiff
assocs: add map>alist which is { } map>assoc but could map to an alist type sometime.
authorDoug Coleman <doug.coleman@gmail.com>
Wed, 12 Aug 2015 04:43:58 +0000 (23:43 -0500)
committerDoug Coleman <doug.coleman@gmail.com>
Wed, 12 Aug 2015 04:43:58 +0000 (23:43 -0500)
core/assocs/assocs-docs.factor
core/assocs/assocs-tests.factor
core/assocs/assocs.factor

index 00c9847914fdec22cef25952203a4d0de5162629..31df46f52c4650f0a8913c1941ce291f612699d1 100644 (file)
@@ -167,6 +167,7 @@ $nl
 "Mapping between assocs and sequences:"
 { $subsections
     map>assoc
+    map>alist
     assoc>map
     assoc-map-as
 }
@@ -471,6 +472,12 @@ HELP: map>assoc
 { $values { "seq" sequence } { "quot" { $quotation ( ... elt -- ... key value ) } } { "exemplar" assoc } { "assoc" "a new assoc" } }
 { $description "Applies the quotation to each element of the sequence, and collects the keys and values into a new assoc having the same type as " { $snippet "exemplar" } "." } ;
 
+HELP: map>alist
+{ $values { "seq" sequence } { "quot" { $quotation ( ... elt -- ... key value ) } } { "assoc" "a new assoc" } }
+{ $description "Applies the quotation to each element of the sequence, and collects the keys and values into a new alist." } ;
+
+{ map>assoc map>alist } related-words
+
 HELP: assoc>map
 { $values { "assoc" assoc } { "quot" { $quotation ( ... key value -- ... elt ) } } { "exemplar" sequence } { "seq" "a new sequence" } }
 { $description "Applies the quotation to each entry of the assoc and collects the results into a new sequence of the same type as the exemplar." } ;
index f780adbca2a94033e84984eb178f9e4e574adab0..eb0d3b8ab22f59ab287f1ee84fd2c4e6d8be7ae1 100644 (file)
@@ -148,6 +148,12 @@ unit-test
     double-array{ 1.0 2.0 } [ dup ] H{ } map>assoc
 ] unit-test
 
+{
+    { { 1.0 1.0 } { 2.0 2.0 } }
+} [
+    double-array{ 1.0 2.0 } [ dup ] { } map>assoc
+] unit-test
+
 { { 3 } } [
     [
         3
index 8294c8c83c0a675e999653545857d6cd5f3421e3..eb64445798e7a193e19e46e5e89c13ec57bff0ab 100644 (file)
@@ -201,6 +201,9 @@ M: assoc values [ nip ] { } assoc>map ;
         [ [ swapd set-at ] curry compose each ] keep
     ] if ; inline
 
+: map>alist ( ... seq quot: ( ... elt -- ... key value ) exemplar -- ... alist )
+    { } map>assoc ; inline
+
 : extract-keys ( seq assoc -- subassoc )
     [ [ dupd at ] curry ] keep map>assoc ;