On this page:
immutable-hash?
mutable-hash?
empty-immutable-hash?
nonempty-immutable-hash?
into-hash
into-mutable-hash
combine-into-hash
combine-into-mutable-hash
empty-hash
hash-ref-safe
hash-set-entry
hash-set-entry!
in-hash-entries
in-mutable-hash-entries
hash-key-set
8.0

4.8 Hash Tables

 (require rebellion/collection/hash) package: rebellion

procedure

(immutable-hash? v)  boolean?

  v : any/c

procedure

(mutable-hash? v)  boolean?

  v : any/c
Convenience predicates for immutable and mutable hash tables, respectively. Both predicates imply hash?.

procedure

(empty-immutable-hash? v)  boolean?

  v : any/c

procedure

(nonempty-immutable-hash? v)  boolean?

  v : any/c
Convenience predicates for empty and nonempty immutable hash tables, respectively. Both predicates imply immutable-hash?.

A pair of reducers that build either an immutable hash table or a mutable hash table from a sequence of entries. Duplicate keys are not allowed, and attempting to reduce a sequence containing duplicate keys will raise exn:fail:contract.

Example:
> (for/reducer into-hash
               ([str (immutable-string-split "The quick brown fox")])
    (entry (string-downcase str)
           (immutable-string-length str)))

'#hash(("brown" . 5) ("fox" . 3) ("quick" . 5) ("the" . 3))

procedure

(combine-into-hash value-combiner)

  (reducer/c entry? immutable-hash?)
  value-combiner : (-> any/c any/c any/c)

procedure

(combine-into-mutable-hash value-combiner)

  (reducer/c entry? mutable-hash?)
  value-combiner : (-> any/c any/c any/c)
Constructs a reducer that combines a sequence of entries into either an immutable hash table or a mutable hash table, respectively. Values for duplicate keys are combined using value-combiner.

Example:
> (reduce (combine-into-hash immutable-string-append)
          (entry 1 "apple")
          (entry 1 ".")
          (entry 1 "banana")
          (entry 2 "orange")
          (entry 2 ".")
          (entry 2 "grape"))

'#hash((1 . "apple.banana") (2 . "orange.grape"))

)

The empty (immutable) hash table.

procedure

(hash-ref-safe h k)  option?

  h : immutable-hash?
  k : any/c
Returns an option containing the value mapped by k in h, returning absent if no such value exists.

Examples:
> (hash-ref-safe (hash 'a 42) 'a)

(present 42)

> (hash-ref-safe (hash 'a 42) 'b)

#<absent>

procedure

(hash-set-entry h e)  immutable-hash?

  h : immutable-hash?
  e : entry?
Like hash-set, but accepting a single entry argument instead of separate key and value arguments.

procedure

(hash-set-entry! h e)  void?

  h : mutable-hash?
  e : entry?
Like hash-set!, but accepting a single entry argument instead of separate key and value arguments.

procedure

(in-hash-entries h)  (sequence/c entry?)

  h : immutable-hash?
Returns a sequence of the entries in h.

procedure

(in-mutable-hash-entries h)  (sequence/c entry?)

  h : mutable-hash?
Returns a sequence of the entries in h.

procedure

(hash-key-set h)  set?

  h : immutable-hash?
Returns a set containing the keys of h.

Example:
> (hash-key-set (hash 'a 1 'b 2 'c 3 'd 4))

(set 'c 'b 'a 'd)