4.2 Entries
(require rebellion/collection/entry) | package: rebellion |
An entry is a key-value pair representing a mapping in a dictionary-like collection, also called a bicollection. Entries are semantically equivalent to pairs, but the two components of an entry are called the key and the value and are retrieved with the entry-key and entry-value functions. Use entries instead of pairs when working with dicitonary-like types, as a collection of entry? values has a clearer intended purpose than a collection of pair? values.
Additionally, entry can be used as a match expander with match.
procedure
(entry-value e) → any/c
e : entry?
> (entry-value (entry "banana" 'yellow)) 'yellow
procedure
(bisecting key-function value-function)
→ (transducer/c any/c entry?) key-function : (-> any/c any/c) value-function : (-> any/c any/c)
(define-record-type book (title author)) (define library (list (book #:title "War and Peace" #:author "Leo Tolstoy") (book #:title "To the Lighthouse" #:author "Virginia Woolf") (book #:title "Frankenstein" #:author "Mary Shelley")))
> (transduce library (bisecting book-author book-title) #:into into-hash)
'#hash(("Leo Tolstoy" . "War and Peace")
("Mary Shelley" . "Frankenstein")
("Virginia Woolf" . "To the Lighthouse"))
(define-record-type book (title author)) (define library (list (book #:title "War and Peace" #:author "Leo Tolstoy") (book #:title "To the Lighthouse" #:author "Virginia Woolf") (book #:title "Frankenstein" #:author "Mary Shelley")))
> (transduce library (indexing book-title) #:into into-hash)
(hash
"Frankenstein"
(book #:author "Mary Shelley" #:title "Frankenstein")
"To the Lighthouse"
(book #:author "Virginia Woolf" #:title "To the Lighthouse")
"War and Peace"
(book #:author "Leo Tolstoy" #:title "War and Peace"))
procedure
(mapping-keys key-function) → (transducer/c entry? entry?)
key-function : (-> any/c any/c)
> (transduce (in-hash-entries (hash "apPLe" 1 "BaNaNa" 2)) (mapping-keys string-titlecase) #:into into-hash) '#hash(("Apple" . 1) ("Banana" . 2))
procedure
(mapping-values value-function) → (transducer/c entry? entry?)
value-function : (-> any/c any/c)
> (transduce (in-hash-entries (hash "car" 1 "bus" 2 "plane" 3 "boat" 4)) (mapping-values sqr) #:into into-hash) '#hash(("boat" . 16) ("bus" . 4) ("car" . 1) ("plane" . 9))
procedure
(filtering-keys key-predicate) → (transducer/c entry? entry?)
key-predicate : predicate/c
> (transduce (in-hash-entries (hash "the" 0 "quick" 1 'brown 2 'fox 3)) (filtering-keys symbol?) #:into into-hash) '#hash((brown . 2) (fox . 3))
procedure
(filtering-values value-predicate)
→ (transducer/c entry? entry?) value-predicate : predicate/c
> (transduce (in-hash-entries (hash 'a 1 'b 2 'c 3 'd 4)) (filtering-values even?) #:into into-hash) '#hash((b . 2) (d . 4))
procedure
(append-mapping-keys key-sequence-function)
→ (transducer/c entry? entry?) key-sequence-function : (-> any/c (sequence/c any/c))
> (transduce (in-hash-entries (hash "hello" 'hello "world" 'world)) (append-mapping-keys in-string) (deduplicating #:key entry-key) #:into into-hash)
'#hash((#\d . world)
(#\e . hello)
(#\h . hello)
(#\l . world)
(#\o . world)
(#\r . world)
(#\w . world))
procedure
(append-mapping-values value-sequence-function)
→ (transducer/c entry? entry?) value-sequence-function : (-> any/c (sequence/c any/c))
> (transduce (in-hash-entries (hash 'apple (set 1 2 3) 'banana (set 4 5 6))) (append-mapping-values in-immutable-set) #:into into-set)
(set
(entry 'apple 2)
(entry 'apple 3)
(entry 'banana 5)
(entry 'banana 4)
(entry 'banana 6)
(entry 'apple 1))
value
> (transduce (list 'a 1 'b 2 'c 3) batching-into-entries #:into into-set) (set (entry 'a 1) (entry 'b 2) (entry 'c 3))
> (transduce (list 'a 1 'b 2 'c) batching-into-entries #:into into-set) batching-into-entries: odd number of sequence elements
last key could not be paired with a value
last key: 'c
procedure
(grouping value-reducer) → (transducer/c entry? entry?)
value-reducer : reducer?