Adding dict-has-key? and updating unstable
This commit is contained in:
parent
958c771ec8
commit
d047557dc6
|
@ -9,6 +9,7 @@
|
||||||
dict-can-remove-keys?
|
dict-can-remove-keys?
|
||||||
dict-can-functional-set?
|
dict-can-functional-set?
|
||||||
|
|
||||||
|
dict-has-key?
|
||||||
dict-ref
|
dict-ref
|
||||||
dict-ref!
|
dict-ref!
|
||||||
dict-set!
|
dict-set!
|
||||||
|
@ -192,6 +193,10 @@
|
||||||
#t))
|
#t))
|
||||||
(raise-type-error 'dict-can-functional-set? "dict" d)))
|
(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
|
(define dict-ref
|
||||||
(case-lambda
|
(case-lambda
|
||||||
[(d key)
|
[(d key)
|
||||||
|
|
|
@ -182,6 +182,21 @@ earlier mappings.
|
||||||
(dict-set* '((a . "apple") (b . "beer")) 'b "banana" 'b "balistic")
|
(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?]
|
@defproc[(dict-ref [dict dict?]
|
||||||
[key any/c]
|
[key any/c]
|
||||||
|
|
|
@ -12,7 +12,9 @@
|
||||||
(test #t dict? d)
|
(test #t dict? d)
|
||||||
|
|
||||||
(test 'one dict-ref d 1)
|
(test 'one dict-ref d 1)
|
||||||
|
(test #t dict-has-key? d 1)
|
||||||
(test 'nope dict-ref d 100 'nope)
|
(test 'nope dict-ref d 100 'nope)
|
||||||
|
(test #f dict-has-key? d 100)
|
||||||
(test 'nope dict-ref d 100 (lambda () 'nope))
|
(test 'nope dict-ref d 100 (lambda () 'nope))
|
||||||
|
|
||||||
(test #t ormap values (dict-map d (lambda (k v) (equal? k orig-one))))
|
(test #t ormap values (dict-map d (lambda (k v) (equal? k orig-one))))
|
||||||
|
|
|
@ -58,10 +58,7 @@
|
||||||
(test-suite "Accessors"
|
(test-suite "Accessors"
|
||||||
(test-suite "dict-empty?"
|
(test-suite "dict-empty?"
|
||||||
(test (check-true (dict-empty? '())))
|
(test (check-true (dict-empty? '())))
|
||||||
(test (check-false (dict-empty? '([1 . a] [2 . b])))))
|
(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-suite "Combination"
|
(test-suite "Combination"
|
||||||
(test-suite "dict-union"
|
(test-suite "dict-union"
|
||||||
(test-ok (dict-union '([1 . one] [2 . two]) '([3 . three] [4 . four]))
|
(test-ok (dict-union '([1 . one] [2 . two]) '([3 . three] [4 . four]))
|
||||||
|
|
|
@ -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)
|
(define (dict-empty? dict)
|
||||||
(= (dict-count dict) 0))
|
(= (dict-count dict) 0))
|
||||||
|
|
||||||
|
@ -202,7 +190,7 @@
|
||||||
;;
|
;;
|
||||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||||
|
|
||||||
(provide dict/c dict-has-key? dict-ref!)
|
(provide dict/c)
|
||||||
(provide/contract
|
(provide/contract
|
||||||
[dict-empty? (-> dict? boolean?)]
|
[dict-empty? (-> dict? boolean?)]
|
||||||
[empty-dict
|
[empty-dict
|
||||||
|
@ -233,8 +221,6 @@
|
||||||
(->d ([table dict?] [key any/c]) ()
|
(->d ([table dict?] [key any/c]) ()
|
||||||
#:pre-cond (dict-has-key? table key)
|
#:pre-cond (dict-has-key? table key)
|
||||||
[_ any/c])]
|
[_ any/c])]
|
||||||
[dict-domain (-> dict? list?)]
|
|
||||||
[dict-range (-> dict? list?)]
|
|
||||||
[dict-union (->* [(and/c dict? dict-can-functional-set?)]
|
[dict-union (->* [(and/c dict? dict-can-functional-set?)]
|
||||||
[#:combine
|
[#:combine
|
||||||
(-> any/c any/c any/c)
|
(-> any/c any/c any/c)
|
||||||
|
|
|
@ -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}
|
@section{Dictionary Combinations}
|
||||||
|
|
||||||
@defproc[(dict-union [d0 (and/c dict? dict-can-functional-set?)]
|
@defproc[(dict-union [d0 (and/c dict? dict-can-functional-set?)]
|
||||||
|
|
Loading…
Reference in New Issue
Block a user