4.11 Association Lists
(require rebellion/collection/association-list) | |
package: rebellion |
An association list is a collection of key-value mappings, where multiple values for the same key are allowed. Distinct key-value mappings are unordered, but order is preserved for mappings with the same key. The implementation of association lists behaves similarly to a hash mapping keys to nonempty lists, but the interface is based on a single flattened collection of key-value pairs.
procedure
(association-list? v) → boolean?
v : any/c
procedure
(association-list k v ... ...) → association-list?
k : any/c v : any/c
> (define assoc (association-list 'weasley 'fred 'weasley 'george 'potter 'harry 'granger 'hermione 'weasley 'ron 'potter 'lily)) > (association-list-ref assoc 'weasley) '#(fred george ron)
> (association-list-keys assoc) (multiset 'granger 'weasley 'weasley 'weasley 'potter 'potter)
> (association-list-contains-value? assoc 'harry) #t
procedure
(association-list-size assoc) → natural?
assoc : association-list?
> (association-list-size (association-list 'a 1 'b 2)) 2
> (association-list-size (association-list 'a 1 'a 2 'a 3)) 3
> (association-list-size (association-list 'a 1 'a 1 'a 1)) 3
procedure
(association-list-ref assoc k) → immutable-vector?
assoc : association-list? k : any/c
> (association-list-ref (association-list 'a 1 'b 2) 'a) '#(1)
> (association-list-ref (association-list 'a 1 'b 2 'a 3) 'a) '#(1 3)
> (association-list-ref (association-list 'a 1 'b 2 'b 2) 'b) '#(2 2)
> (association-list-ref (association-list 'a 1 'a 1) 'a) '#(1 1)
procedure
(association-list-keys assoc) → multiset?
assoc : association-list?
> (association-list-keys (association-list 'a 1 'b 2 'c 3)) (multiset 'a 'b 'c)
> (association-list-keys (association-list 'a 1 'a 2 'a 3)) (multiset 'a 'a 'a)
> (association-list-keys (association-list 'a 1 'a 1 'b 2 'b 2 'c 3)) (multiset 'a 'a 'b 'b 'c)
procedure
(association-list-unique-keys assoc) → set?
assoc : association-list?
> (association-list-unique-keys (association-list 'a 1 'b 2 'c 3)) (set 'c 'b 'a)
> (association-list-unique-keys (association-list 'a 1 'b 2 'a 3)) (set 'b 'a)
procedure
(association-list-values assoc) → immutable-vector?
assoc : association-list?
> (association-list-values (association-list 'a 1 'b 2 'c 3 'a 1 'a 3 'c 2)) '#(3 2 2 1 1 3)
> (association-list-values (association-list 'a 1 'a 1 'a 1)) '#(1 1 1)
> (association-list-values (association-list 'a 1 'b 2 'a 3 'b 4)) '#(2 4 1 3)
procedure
(association-list-entries assoc)
→ (vectorof entry? #:immutable #true) assoc : association-list?
> (association-list-entries (association-list 'a 1 'b 2 'a 3 'c 4)) (vector (entry 'c 4) (entry 'b 2) (entry 'a 1) (entry 'a 3))
procedure
(association-list-contains-key? assoc k) → boolean?
assoc : association-list? k : any/c
> (define assoc (association-list 'a 1 'b 2 'c 3 'a 4)) > (association-list-contains-key? assoc 'a) #t
> (association-list-contains-key? assoc 'banana) #f
procedure
(association-list-contains-value? assoc v) → boolean?
assoc : association-list? v : any/c
> (define assoc (association-list 'a 1 'b 2 'c 3 'a 4)) > (association-list-contains-key? assoc 3) #f
> (association-list-contains-key? assoc 10) #f
procedure
(association-list-contains-entry? assoc e) → boolean?
assoc : association-list? e : entry?
> (define assoc (association-list 'a 1 'b 2 'c 3 'a 4)) > (association-list-contains-entry? assoc (entry 'a 4)) #t
> (association-list-contains-entry? assoc (entry 'a 2)) #f
> (association-list-contains-entry? assoc (entry 'c 1)) #f
procedure
(association-list->hash assoc)
→
(hash/c any/c nonempty-immutable-vector? #:immutable #true) assoc : association-list?
> (association-list->hash (association-list 'a 1 'b 2 'c 3 'b 4 'b 5 'c 6 'a 7)) '#hash((a . #(1 7)) (b . #(2 4 5)) (c . #(3 6)))
procedure
v : any/c
procedure
v : any/c