Adding dict-has-key? and updating unstable

This commit is contained in:
Jay McCarthy 2010-08-12 09:00:07 -06:00
parent 958c771ec8
commit d047557dc6
6 changed files with 24 additions and 31 deletions

View File

@ -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)

View File

@ -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]

View File

@ -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))))

View File

@ -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]))

View File

@ -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)

View File

@ -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?)]