diff --git a/collects/racket/dict.rkt b/collects/racket/dict.rkt index d21f9362b2..666f3b55dc 100644 --- a/collects/racket/dict.rkt +++ b/collects/racket/dict.rkt @@ -29,6 +29,10 @@ in-dict-keys in-dict-values in-dict-pairs + + dict-keys + dict-values + dict->list (rename-out [create-custom-hash make-custom-hash] [create-immutable-custom-hash make-immutable-custom-hash]) @@ -459,6 +463,20 @@ (for ([(k v) (in-dict d)]) (f k v))) +(define (dict-keys d) + (for/list ([k (in-dict-keys d)]) + k)) + +(define (dict-values d) + (for/list ([v (in-dict-values d)]) + v)) + +(define (dict->list d) + (for/list ([k*v (in-dict-pairs d)]) + k*v)) + + + ;; ---------------------------------------- (struct hash-box (key)) diff --git a/collects/scribblings/reference/dicts.scrbl b/collects/scribblings/reference/dicts.scrbl index c811a74596..a2a5bd337d 100644 --- a/collects/scribblings/reference/dicts.scrbl +++ b/collects/scribblings/reference/dicts.scrbl @@ -404,7 +404,35 @@ key and value as separate values for each element). p) ]} +@defproc[(dict-keys [dict dict?]) list?]{ +Returns a list of the keys from +@scheme[dict] in an unspecified order. +@examples[ +#:eval dict-eval +(define h #hash((a . "apple") (b . "banana"))) +(dict-keys h) +]} + +@defproc[(dict-values [dict dict?]) list?]{ +Returns a list of the values from +@scheme[dict] in an unspecified order. + +@examples[ +#:eval dict-eval +(define h #hash((a . "apple") (b . "banana"))) +(dict-values h) +]} + +@defproc[(dict->list [dict dict?]) list?]{ +Returns a list of the associations from +@scheme[dict] in an unspecified order. + +@examples[ +#:eval dict-eval +(define h #hash((a . "apple") (b . "banana"))) +(dict->list h) +]} @defthing[prop:dict struct-type-property?]{ diff --git a/collects/tests/racket/dict.rktl b/collects/tests/racket/dict.rktl index 5fb3787514..b4850aeaaf 100644 --- a/collects/tests/racket/dict.rktl +++ b/collects/tests/racket/dict.rktl @@ -23,6 +23,10 @@ (test can-remove? dict-can-remove-keys? d) (test can-update? dict-can-functional-set? d) + (test (dict-map d cons) 'dict->list (dict->list d)) + (test (dict-map d (λ (k v) k)) 'dict-keys (dict-keys d)) + (test (dict-map d (λ (k v) v)) 'dict-values (dict-values d)) + (test (dict-map d cons) 'in-dict (for/list ([(k v) (in-dict d)]) (cons k v)))