6.7 mischief/dict: Dictionaries
| (require mischief/dict) | package: mischief | 
procedure
(dict-ref? dict key [ #:success success #:failure failure]) → any dict : dict? key : any/c success : (-> any/c any) = identity 
failure : any/c = 
(lambda {} (error 'dict-ref? "key ~v not found in dict ~v" key dict)) 
Looks up key in dict.  If dict has a mapping for
key, calls success with the associated value.  Otherwise,
invokes failure if it is a procedure, and returns it otherwise.
Examples:
> (define (search table name) (dict-ref? table name #:failure (lambda {} (gensym name)) #:success (lambda {id} (search table id)))) > (define table (hash 'y 'x)) > (search table 'x) 'x153529
> (search table 'y) 'x153530
> (search table 'z) 'z153531
procedure
(dict-update? dict key #:transform proc [ #:success success #:failure failure]) → dict? dict : dict? key : any/c proc : (-> any/c any/c) success : (-> any/c any/c) = identity 
failure : any/c = 
(lambda {} (error 'dict-update? "key ~v not found in dict ~v" key dict)) 
Updates the value for key in dict.  If key is bound
to some value x in dict, updates the binding to
(proc (success x)).  Otherwise, binds key to
(proc (failure)) if failure is a procedure, and
(proc failure) otherwise.
Examples:
> (define (insert key val table) (dict-update? table key #:transform (lambda {vals} (cons val vals)) #:success (lambda {vals} (remove val vals)) #:failure (lambda {} '()))) 
> (define table (hash 'a '(x y z))) 
> (insert 'a 'z (insert 'b 'x table)) '#hash((b . (x)) (a . (z x y)))
procedure
(dict-add d0 d ... [ #:combine combine #:combine/key combine/key]) → dict? d0 : dict? d : dict? combine : (or/c #false (-> any/c any/c any/c)) = #false 
combine/key : (or/c #false (-> any/c any/c any/c any/c)) = 
(if combine (lambda {k v1 v2} (combine v1 v2)) #false) 
Adds the bindings in each d to d0.  When one key k
has two values v1 and v2, binds k to
(combine/key k v1 v2) in the result.
Example:
procedure
(dict-subtract d0 d ...) → dict?
d0 : dict? d : dict? 
Removes the bindings for the keys in each d from d0, if they
exist.
Example:
> (dict-subtract (hasheq 'a 1 'b 2 'c 3) (hash 'a "team" 'b "hive")) '#hasheq((c . 3))
procedure
(dict-set-all d #:value value seq ...) → dict?
d : dict? value : any/c seq : sequence? 
Binds every value x from each sequence seq in d to
(value x) if x is a procedure, and to x otherwise.
Example:
> (dict-set-all (hash) #:value symbol->string '(a b c)) '#hash((c . "c") (b . "b") (a . "a"))
procedure
(dict-remove-all d seq ...) → dict?
d : dict? seq : sequence? 
Removes every value x in each sequence seq from d.
Example:
> (dict-remove-all (hash 'a 1 'b 2 'c 3) '(a b)) '#hash((c . 3))
procedure
(dict->procedure dict [#:failure failure]) → (-> any/c any)
dict : dict? 
failure : any/c = 
(lambda {key} (error 'dict->procedure "key ~v not found in dict ~v" key dict)) 
Returns a procedure that looks up its argument in dict and returns the
associated value.  If a key k is not found, calls (failure k)
if it is a procedure and returns failure otherwise.
Examples:
> (define lookup (dict->procedure (hash 'a 1 'b 2 'c 3) #:failure 0)) > (lookup 'a) 1
> (lookup 'x) 0