Adding dict-keys,values,->list

This commit is contained in:
Jay McCarthy 2010-08-12 08:23:38 -06:00
parent f23daa3aea
commit a8fc09b49a
3 changed files with 50 additions and 0 deletions

View File

@ -30,6 +30,10 @@
in-dict-values in-dict-values
in-dict-pairs in-dict-pairs
dict-keys
dict-values
dict->list
(rename-out [create-custom-hash make-custom-hash] (rename-out [create-custom-hash make-custom-hash]
[create-immutable-custom-hash make-immutable-custom-hash]) [create-immutable-custom-hash make-immutable-custom-hash])
make-weak-custom-hash) make-weak-custom-hash)
@ -459,6 +463,20 @@
(for ([(k v) (in-dict d)]) (for ([(k v) (in-dict d)])
(f k v))) (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)) (struct hash-box (key))

View File

@ -404,7 +404,35 @@ key and value as separate values for each element).
p) 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?]{ @defthing[prop:dict struct-type-property?]{

View File

@ -23,6 +23,10 @@
(test can-remove? dict-can-remove-keys? d) (test can-remove? dict-can-remove-keys? d)
(test can-update? dict-can-functional-set? 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 (test (dict-map d cons) 'in-dict
(for/list ([(k v) (in-dict d)]) (for/list ([(k v) (in-dict d)])
(cons k v))) (cons k v)))