assocs: add map>alist which is { } map>assoc but could map to an alist type sometime.
parent
00338f62f3
commit
114ba4733a
|
@ -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." } ;
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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 ;
|
||||
|
||||
|
|
Loading…
Reference in New Issue