From 114ba4733a0fcb41130d7cd46fc8f4001f1e62ec Mon Sep 17 00:00:00 2001 From: Doug Coleman Date: Tue, 11 Aug 2015 23:43:58 -0500 Subject: [PATCH] assocs: add map>alist which is { } map>assoc but could map to an alist type sometime. --- core/assocs/assocs-docs.factor | 7 +++++++ core/assocs/assocs-tests.factor | 6 ++++++ core/assocs/assocs.factor | 3 +++ 3 files changed, 16 insertions(+) diff --git a/core/assocs/assocs-docs.factor b/core/assocs/assocs-docs.factor index 00c9847914..31df46f52c 100644 --- a/core/assocs/assocs-docs.factor +++ b/core/assocs/assocs-docs.factor @@ -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." } ; diff --git a/core/assocs/assocs-tests.factor b/core/assocs/assocs-tests.factor index f780adbca2..eb0d3b8ab2 100644 --- a/core/assocs/assocs-tests.factor +++ b/core/assocs/assocs-tests.factor @@ -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 diff --git a/core/assocs/assocs.factor b/core/assocs/assocs.factor index 8294c8c83c..eb64445798 100644 --- a/core/assocs/assocs.factor +++ b/core/assocs/assocs.factor @@ -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 ;