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
grouping
7.4

4.1 Entries

 (require rebellion/collection/entry) package: rebellion

An entry is a key-value pair representing a mapping in a dictionary-like collection. Entries are semantically equivalent to pairs, but use the less generic and more readable names entry-key and entry-value. 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)

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?

  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?

  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?

  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.

procedure

(mapping-values value-function)  transducer?

  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.

procedure

(filtering-keys key-predicate)  transducer?

  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?

  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?

  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?

  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 'apple 1)

 (entry 'apple 2)

 (entry 'banana 6)

 (entry 'banana 4)

 (entry 'apple 3)

 (entry 'banana 5))

procedure

(grouping value-reducer)  transducer?

  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))