diff --git a/collects/racket/dict.rkt b/collects/racket/dict.rkt index 862276229c..cddceffaef 100644 --- a/collects/racket/dict.rkt +++ b/collects/racket/dict.rkt @@ -9,6 +9,7 @@ dict-can-remove-keys? dict-can-functional-set? + dict-has-key? dict-ref dict-ref! dict-set! @@ -192,6 +193,10 @@ #t)) (raise-type-error 'dict-can-functional-set? "dict" d))) +(define (dict-has-key? d k) + (define not-there (gensym)) + (not (eq? not-there (dict-ref d k not-there)))) + (define dict-ref (case-lambda [(d key) diff --git a/collects/scribblings/reference/dicts.scrbl b/collects/scribblings/reference/dicts.scrbl index a488ad0663..adf4ec4093 100644 --- a/collects/scribblings/reference/dicts.scrbl +++ b/collects/scribblings/reference/dicts.scrbl @@ -182,6 +182,21 @@ earlier mappings. (dict-set* '((a . "apple") (b . "beer")) 'b "banana" 'b "balistic") ]} +@defproc[(dict-has-key? [dict dict?] [key any/c]) + boolean?]{ + +Returns @scheme[#t] if @scheme[dict] contains a value for the given +@scheme[key], @scheme[#f] otherwise. + +@examples[ +#:eval dict-eval +(dict-has-key? #hash((a . "apple") (b . "beer")) 'a) +(dict-has-key? #hash((a . "apple") (b . "beer")) 'c) +(dict-has-key? '((a . "apple") (b . "banana")) 'b) +(dict-has-key? #("apple" "banana") 1) +(dict-has-key? #("apple" "banana") 3) +(dict-has-key? #("apple" "banana") -3) +]} @defproc[(dict-ref [dict dict?] [key any/c] diff --git a/collects/tests/racket/dict.rktl b/collects/tests/racket/dict.rktl index a449ca8047..e708254635 100644 --- a/collects/tests/racket/dict.rktl +++ b/collects/tests/racket/dict.rktl @@ -12,7 +12,9 @@ (test #t dict? d) (test 'one dict-ref d 1) + (test #t dict-has-key? d 1) (test 'nope dict-ref d 100 'nope) + (test #f dict-has-key? d 100) (test 'nope dict-ref d 100 (lambda () 'nope)) (test #t ormap values (dict-map d (lambda (k v) (equal? k orig-one)))) diff --git a/collects/tests/unstable/dict.rkt b/collects/tests/unstable/dict.rkt index 57514f644b..c8b89cc689 100644 --- a/collects/tests/unstable/dict.rkt +++ b/collects/tests/unstable/dict.rkt @@ -58,10 +58,7 @@ (test-suite "Accessors" (test-suite "dict-empty?" (test (check-true (dict-empty? '()))) - (test (check-false (dict-empty? '([1 . a] [2 . b]))))) - (test-suite "dict-has-key?" - (test-ok (check-equal? (dict-has-key? '([1 . one] [2 . two]) 1) #t)) - (test-ok (check-equal? (dict-has-key? '([1 . one] [2 . two]) 3) #f)))) + (test (check-false (dict-empty? '([1 . a] [2 . b])))))) (test-suite "Combination" (test-suite "dict-union" (test-ok (dict-union '([1 . one] [2 . two]) '([3 . three] [4 . four])) diff --git a/collects/unstable/dict.rkt b/collects/unstable/dict.rkt index 9bb247dbab..0ad366de85 100644 --- a/collects/unstable/dict.rkt +++ b/collects/unstable/dict.rkt @@ -8,18 +8,6 @@ ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -(define dict-has-key? - (let () - (with-contract - dict-has-key? - ([dict-has-key? (-> dict? any/c boolean?)]) - (define (dict-has-key? dict key) - (let/ec return - (dict-ref dict key (lambda () (return #f))) - #t))) - dict-has-key?)) -;; Ryan: Why the with-contract region? Why not provide/contract? - (define (dict-empty? dict) (= (dict-count dict) 0)) @@ -202,7 +190,7 @@ ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -(provide dict/c dict-has-key? dict-ref!) +(provide dict/c) (provide/contract [dict-empty? (-> dict? boolean?)] [empty-dict @@ -233,8 +221,6 @@ (->d ([table dict?] [key any/c]) () #:pre-cond (dict-has-key? table key) [_ any/c])] - [dict-domain (-> dict? list?)] - [dict-range (-> dict? list?)] [dict-union (->* [(and/c dict? dict-can-functional-set?)] [#:combine (-> any/c any/c any/c) diff --git a/collects/unstable/scribblings/dict.scrbl b/collects/unstable/scribblings/dict.scrbl index 1b7871f71f..9b479b4256 100644 --- a/collects/unstable/scribblings/dict.scrbl +++ b/collects/unstable/scribblings/dict.scrbl @@ -142,18 +142,6 @@ Reports whether @scheme[d] is empty (has no keys). } -@defproc[(dict-has-key? [d dict?] [k any/c]) boolean?]{ - -Reports whether @scheme[d] has an entry for @scheme[k]. - -@defexamples[ -#:eval (eval/require 'racket/dict 'unstable/dict) -(dict-has-key? '([1 . one] [2 . two] [3 . three]) 2) -(dict-has-key? '([1 . one] [2 . two] [3 . three]) 4) -] - -} - @section{Dictionary Combinations} @defproc[(dict-union [d0 (and/c dict? dict-can-functional-set?)]