assocs: add map>alist which is { } map>assoc but could map to an alist type sometime.

db4
Doug Coleman 2015-08-11 23:43:58 -05:00
parent 00338f62f3
commit 114ba4733a
3 changed files with 16 additions and 0 deletions

View 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." } ;

View 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

View 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 ;