6 Dictionaries
(require unstable/dict) |
This module provides tools for manipulating dictionary values.
6.1 Dictionary Constructors
| |||||||||||||||||||||
mutable? : boolean? = weak? | |||||||||||||||||||||
weak? : boolean? = #f | |||||||||||||||||||||
compare : (or/c 'eq 'eqv 'equal) = equal |
Examples: |
> (empty-dict) |
'#hash() |
> (empty-dict #:mutable? #t) |
'#hash() |
> (empty-dict #:weak? #t) |
'#hash() |
> (empty-dict #:compare 'eqv) |
'#hasheqv() |
| ||||||||||||||||||||||||||||
d : dict? | ||||||||||||||||||||||||||||
mutable? : boolean? = weak? | ||||||||||||||||||||||||||||
weak? : boolean? = #f | ||||||||||||||||||||||||||||
compare : (or/c 'eq 'eqv 'equal) = equal |
Examples: |
> (make-dict '([1 . one] [2 . two])) |
'#hash((1 . one) (2 . two)) |
> (make-dict '([1 . one] [2 . two]) #:mutable? #t) |
'#hash((1 . one) (2 . two)) |
> (make-dict '([1 . one] [2 . two]) #:weak? #t) |
'#hash((1 . one) (2 . two)) |
> (make-dict '([1 . one] [2 . two]) #:compare 'eqv) |
'#hasheqv((1 . one) (2 . two)) |
| |||||||||||||||||||||||||||||||||||
equiv? : (-> any/c any/c any/c) | |||||||||||||||||||||||||||||||||||
hash-primary : (-> any/c exact-integer?) = (lambda (x) 0) | |||||||||||||||||||||||||||||||||||
hash-secondary : (-> any/c exact-integer?) = (lambda (x) 0) | |||||||||||||||||||||||||||||||||||
mutable? : boolean? = weak? | |||||||||||||||||||||||||||||||||||
weak? : boolean? = #f |
Examples: | ||
| ||
> (dict-set! table 1 'one) | ||
> (dict-set! table 2 'two) | ||
| ||
'((2 . two) (1 . one)) |
6.2 Dictionary Lookup
Examples: | ||
| ||
> (dict-set! d 1 'one) | ||
> (dict-set! d 2 'two) | ||
> d | ||
'#hash((1 . one) (2 . two)) | ||
> (dict-ref! d 2 'dos) | ||
'two | ||
> d | ||
'#hash((1 . one) (2 . two)) | ||
> (dict-ref! d 3 'tres) | ||
'tres | ||
> d | ||
'#hash((1 . one) (2 . two) (3 . tres)) | ||
> (dict-ref! d 4 gensym) | ||
'g51036 | ||
> d | ||
'#hash((1 . one) (2 . two) (3 . tres) (4 . g51036)) |
(dict-ref/check d k) → any/c |
d : dict? |
k : (lambda (k) (dict-has-key? d k)) |
Example: |
> (dict-ref/check '([1 . one] [2 . two] [3 . three]) 2) |
'two |
(dict-ref/identity d k) → any/c |
d : dict? |
k : any/c |
Examples: |
> (dict-ref/identity '([1 . one] [2 . two] [3 . three]) 2) |
'two |
> (dict-ref/identity '([1 . one] [2 . two] [3 . three]) 4) |
4 |
(dict-ref/default d k v) → any/c |
d : dict? |
k : any/c |
v : any/c |
Examples: |
> (dict-ref/default '([1 . one] [2 . two] [3 . three]) 2 'other) |
'two |
> (dict-ref/default '([1 . one] [2 . two] [3 . three]) 4 'other) |
'other |
Examples: |
> (dict-ref/failure '([1 . one] [2 . two] [3 . three]) 2 gensym) |
'two |
> (dict-ref/failure '([1 . one] [2 . two] [3 . three]) 4 gensym) |
'g51126 |
6.3 Dictionary Accessors
(dict-empty? d) → boolean? |
d : dict? |
Examples: |
> (dict-empty? '()) |
#t |
> (dict-empty? '([1 . one] [2 . two])) |
#f |
(dict-has-key? d k) → boolean? |
d : dict? |
k : any/c |
Examples: |
> (dict-has-key? '([1 . one] [2 . two] [3 . three]) 2) |
#t |
> (dict-has-key? '([1 . one] [2 . two] [3 . three]) 4) |
#f |
(dict-domain d) → list? |
d : dict? |
Example: |
> (dict-domain '([1 . one] [2 . two] [3 . three])) |
'(1 2 3) |
(dict-range d) → list? |
d : dict? |
Example: |
> (dict-range '([1 . one] [2 . two] [3 . three])) |
'(one two three) |
6.4 Dictionary Combinations
| ||||||||||||||||
→ (and/c dict? dict-can-functional-set?) | ||||||||||||||||
d0 : (and/c dict? dict-can-functional-set?) | ||||||||||||||||
d : dict? | ||||||||||||||||
| ||||||||||||||||
|
Examples: | |||
> (dict-union '([1 . one]) '([2 . two]) '([3 . three])) | |||
'((1 . one) (2 . two) (3 . three)) | |||
| |||
'((1 one uno ein une) (2 two dos zwei deux)) |
| ||||||||||||||||||||||||||||
d0 : (and/c dict? dict-mutable?) | ||||||||||||||||||||||||||||
d : dict? | ||||||||||||||||||||||||||||
| ||||||||||||||||||||||||||||
|
Examples: | |||
| |||
> d | |||
'#hash() | |||
> (dict-union! d '([1 one uno] [2 two dos])) | |||
> d | |||
'#hash((1 . (one uno)) (2 . (two dos))) | |||
| |||
> d | |||
'#hash((1 . (one uno ein une)) (2 . (two dos zwei deux))) |
6.5 Dictionary Structure Properties
| ||||||||||||||||||||||||||||||||||||||||||
unwrap : (-> (and/c dict? pred) dict?) | ||||||||||||||||||||||||||||||||||||||||||
wrap : (-> dict? (and/c dict? pred)) = (lambda (x) x) | ||||||||||||||||||||||||||||||||||||||||||
pred : (-> any/c boolean?) = (lambda (x) #t) | ||||||||||||||||||||||||||||||||||||||||||
mutable? : boolean? = weak? | ||||||||||||||||||||||||||||||||||||||||||
mutable? : boolean? = #f | ||||||||||||||||||||||||||||||||||||||||||
functional? : boolean? = #t |
Examples: | ||||||||
| ||||||||
> (dict? (make-table '([1 . one] [2 . two]))) | ||||||||
#t | ||||||||
> (dict-ref (make-table '([1 . one] [2 . two])) 1) | ||||||||
'one | ||||||||
> (dict-set (make-table '([1 . one] [2 . two])) 3 'three) | ||||||||
(table ( (1 . one) (2 . two) (3 . three))) |
6.6 Contracted Dictionaries
This library re-provides dict/c from unstable/contract.