On this page:
entry?
entry
entry-key
entry-value
bisecting
indexing
mapping-keys
mapping-values
filtering-keys
filtering-values
append-mapping-keys
append-mapping-values
batching-into-entries
grouping
7.7

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.

procedure

(entry? v)  boolean?

  v : any/c
A predicate for entries.

procedure

(entry k v)  entry?

  k : any/c
  v : any/c
Constructs an entry.

Examples:
> (entry "apple" 'red)

(entry "apple" 'red)

> (entry "banana" 'yellow)

(entry "banana" 'yellow)

Additionally, entry can be used as a match expander with match.

Example:
> (match (entry "apple" 'red)
    [(entry k v) (entry v k)])

(entry 'red "apple")

procedure

(entry-key e)  any/c

  e : entry?
Returns the key of e.

Example:
> (entry-key (entry "apple" 'red))

"apple"

procedure

(entry-value e)  any/c

  e : entry?
Returns the value in e.

Example:
> (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)
Constructs a transducer that transforms each element of a sequence into an entry, by applying key-function and value-function to the element to extract the key and value of the entry.

Examples:
(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"))

procedure

(indexing key-function)  (transducer/c any/c entry?)

  key-function : (-> any/c any/c)
Constructs a transducer that transforms a sequence of elements into a sequence of entries keyed by key-function.

Examples:
(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)
Like mapping, but the sequence must be made of entries and key-function is applied to the key of each entry.

Example:
> (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)
Like mapping, but the sequence must be made of entries and value-function is applied to the key of each entry.

Example:
> (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
Like filtering, but the sequence must be made of entries and the key of each entry must satisfy key-predicate.

Example:
> (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
Like filtering, but the sequence must be made of entries and the value of each entry must satisfy value-predicate.

Example:
> (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))
Constructs a transducer that transforms a sequence of entries by applying key-sequence-function to the key of each entry, then emitting one entry per key in the sequence returned by key-sequence-function.

Example:
> (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 . hello)

       (#\o . hello)

       (#\r . world)

       (#\w . world))

procedure

(append-mapping-values value-sequence-function)

  (transducer/c entry? entry?)
  value-sequence-function : (-> any/c (sequence/c any/c))
Constructs a transducer that transforms a sequence of entries by applying value-sequence-function to the value of each entry, then emitting one entry per value in the sequence returned by value-sequence-function.

Example:
> (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 'banana 5)

 (entry 'apple 2)

 (entry 'apple 3)

 (entry 'banana 4)

 (entry 'banana 6)

 (entry 'apple 1))

A transducer that transforms a flat sequences of alternating keys and values into a sequence of entries. The sequence must contain an even number of elements, otherwise a contract failure is raised.

Examples:
> (transduce (list 'a 1 'b 2 'c 3)
             batching-into-entries
             #:into into-set)

(set (entry 'c 3) (entry 'a 1) (entry 'b 2))

> (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?
Constructs a transducer that transforms a sequence of entries by merging entries with equal keys. For each key, the values of all entries with that key are combined using value-reducer. If value-reducer finishes early, its result is emitted and any remaining values for that key are discarded.

Example:
> (transduce (multidict 'a 1 'b 2 'a 3 'b 4 'a 5)
             (grouping into-sum)
             #:into into-hash)

'#hash((a . 9) (b . 6))