On this page:
make-similar-hash
hash-comparison-same?
hash-keys-same?
hash-ref-maybe
hash-set-maybe
hash-kv-map-sorted
hash-kv-bind
hash-kv-map-maybe
hash-kv-map
hash-kv-any
hash-kv-all
hash-kv-each
hash-v-map-maybe
hash-v-map
hash-v-any
hash-v-all
hash-v-each

7 Utilities for Hash Tables

 (require lathe-comforts/hash)
  package: lathe-comforts-lib

procedure

(make-similar-hash example assocs)  hash?

  example : hash?
  assocs : (listof pair?)
Returns a hash table with the same key-comparison procedure, key-holding strength, and mutability as example but with entries populated from assocs instead. If assocs contains duplicate keys, the last entry in the list takes precedence.

procedure

(hash-comparison-same? a b)  boolean?

  a : hash?
  b : hash?
Returns whether the two given hash tables have the same comparison procedure (equal?, eqv?, or eq?).

procedure

(hash-keys-same? a b)  boolean?

  a : hash?
  b : hash?
Returns whether the two given hash tables have the same set of keys according to their comparison procedure. If the two hash tables don’t even have the same comparison procedure, the exn:fail:contract exception is raised.

If the thread performing this operation is terminated and the given hash tables use eqv? or equal? as their comparison procedure, all current and future operations on those hash tables may block indefinitely.

If either of the given hash tables is modified partway through this operation, or if either one is an equal?-based hash table whose keys have been mutated after insertion, the resulting behavior may be unpredictable.

procedure

(hash-ref-maybe hash key)  maybe?

  hash : hash?
  key : any/c
Looks up the given key in the given hash table. Returns a just? of the value found or a nothing? if no entry for that key exists.

If the thread performing this operation is terminated and the given hash table uses eqv? or equal? as its comparison procedure, all current and future operations on that hash table may block indefinitely.

If the given hash table is modified partway through this operation, or if it’s an equal?-based hash table whose keys have been mutated after insertion, the resulting behavior may be unpredictable.

procedure

(hash-set-maybe hash key maybe-value)  hash?

  hash : hash?
  key : any/c
  maybe-value : maybe?
Returns a hash table with the same key-comparison procedure, key-holding strength, and mutability as the given one, where the given key’s mapping has been deleted (if maybe-value is a nothing?) or replaced (if it’s a just? of some value to use as the replacement).

If the given hash table is an equal?-based hash table whose keys have been mutated after insertion, the resulting behavior may be unpredictable.

If the result is an equal?-based hash table and one of its keys is mutated after insertion, it may have unpredictable behavior with other hash-table-related utilities.

procedure

(hash-kv-map-sorted key<? hash func)  list?

  key<? : (-> any/c any/c boolean?)
  hash : hash?
  func : (-> any/c any/c any/c)
Returns a list constructed by converting the given hash table to a list, sorting it in ascending order by comparing keys according to the given key<? behavior, and then transforming each entry in ascending order by calling the given func with the entry’s key and value.

The number of times key<? is called is unspecified. The particular argument values it receives are unspecified, aside from the fact that they are all keys of hash.

If the given hash table is modified while this operation is setting up (before it calls either of the given functions for the first time), the particular list of entries it processes may be unpredictable. Modifications after that will not affect the operation.

procedure

(hash-kv-bind hash func)  hash?

  hash : hash?
  func : (-> any/c any/c hash?)
Returns a hash table constructed by iterating over the given hash table hash’s entries in an unspecified order, calling the given function func with each entry’s key and value, and collecting the entries of the resulting hash tables into a single result hash table.

If multiple results of func have the same key, the last one to be computed takes precedence.

The resulting hash table has the same key-comparison procedure, key-holding strength, and mutability as hash. Each result of func may have its own combination of key-comparison procedure, key-holding strength, and mutability; these properties will be ignored.

If hash is modified while this operation is setting up (before it calls the given function for the first time), the particular list of entries this operation processes may be unpredictable. Modifications after that will not affect the operation.

Likewise, if a result of func is modified between the time it is returned and the time this operation either returns or calls func again, then the particular list of entries this operation collects from that func result may be unpredictable.

If the overall result is an equal?-based hash table and one of its keys is mutated after insertion, it may have unpredictable behavior with other hash-table-related utilities.

procedure

(hash-kv-map-maybe hash func)  hash?

  hash : hash?
  func : (-> any/c any/c maybe?)
Returns a hash table constructed by iterating over the given hash table’s entries in an unspecified order, calling the given function with each entry’s key and value, and collecting the results. If for some input entry, the function returns a nothing?, then there is no corresponding output entry. When the function returns a just?, then there is a corresponding output entry which maps the input entry’s key to the the value of the just?.

The resulting hash table has the same key-comparison procedure, key-holding strength, and mutability as the given one.

If the given hash table is modified while this operation is setting up (before it calls the given function for the first time), the particular list of entries it processes may be unpredictable. Modifications after that will not affect the operation.

If the result is an equal?-based hash table and one of its keys is mutated after insertion, it may have unpredictable behavior with other hash-table-related utilities.

procedure

(hash-kv-map hash func)  hash?

  hash : hash?
  func : (-> any/c any/c any/c)
Returns a hash table with the same keys as the given one. The result is constructed by iterating over the given hash table’s entries in an unspecified order and calling the given function with each entry’s key and value to determine the corresponding result entry’s mapped value.

The resulting hash table has the same key-comparison procedure, key-holding strength, and mutability as the given one.

If the given hash table is modified while this operation is setting up (before it calls the given function for the first time), the particular list of entries it processes may be unpredictable. Modifications after that will not affect the operation.

If the result is an equal?-based hash table and one of its keys is mutated after insertion, it may have unpredictable behavior with other hash-table-related utilities.

procedure

(hash-kv-any hash func)  boolean?

  hash : hash?
  func : (-> any/c any/c boolean?)
Iterates over the given hash table’s entries in an unspecified order and calls the given function on each entry’s key and value, stopping early if the function returns #t. If the function does return #t, then the overall result is #t; otherwise, it’s #f.

If the given hash table is modified partway through this operation, the resulting behavior may be unpredictable.

procedure

(hash-kv-all hash func)  boolean?

  hash : hash?
  func : (-> any/c any/c boolean?)
Iterates over the given hash table’s entries in an unspecified order and calls the given function on each entry’s key and value, stopping early if the function returns #f. If the function does return #f, then the overall result is #f; otherwise, it’s #t.

If the given hash table is modified partway through this operation, the resulting behavior may be unpredictable.

procedure

(hash-kv-each hash body)  void?

  hash : hash?
  body : (-> any/c any/c any)
Iterates over the given hash table’s entries in an unspecified order and calls the given procedure on each entry’s key and value. Ignores the procedure’s results.

If the given hash table is modified partway through this operation, the resulting behavior may be unpredictable.

procedure

(hash-v-map-maybe hash func)  hash?

  hash : hash?
  func : (-> any/c maybe?)
Returns a hash table constructed by iterating over the given hash table’s entries in an unspecified order, calling the given function with each entry’s mapped value, and collecting the results. If for some input entry, the function returns a nothing?, then there is no corresponding output entry. When the function returns a just?, then there is a corresponding output entry which maps the input entry’s key to the the value of the just?.

The resulting hash table has the same key-comparison procedure, key-holding strength, and mutability as the given one.

If the given hash table is modified while this operation is setting up (before it calls the given function for the first time), the particular list of entries it processes may be unpredictable. Modifications after that will not affect the operation.

If the result is an equal?-based hash table and one of its keys is mutated after insertion, it may have unpredictable behavior with other hash-table-related utilities.

procedure

(hash-v-map hash func)  hash?

  hash : hash?
  func : (-> any/c any/c)
Returns a hash table with the same keys as the given one. The result is constructed by iterating over the given hash table’s entries in an unspecified order and calling the given function with each entry’s mapped value to determine the corresponding result entry’s mapped value.

The resulting hash table has the same key-comparison procedure, key-holding strength, and mutability as the given one.

If the given hash table is modified while this operation is setting up (before it calls the given function for the first time), the particular list of entries it processes may be unpredictable. Modifications after that will not affect the operation.

procedure

(hash-v-any hash func)  boolean?

  hash : hash?
  func : (-> any/c boolean?)
Iterates over the given hash table’s mapped values in an unspecified order and calls the given function on each one, stopping early if the function returns #t. If the function does return #t, then the overall result is #t; otherwise, it’s #f.

If the given hash table is modified partway through this operation, the resulting behavior may be unpredictable.

procedure

(hash-v-all hash func)  boolean?

  hash : hash?
  func : (-> any/c boolean?)
Iterates over the given hash table’s mapped values in an unspecified order and calls the given function on each one, stopping early if the function returns #f. If the function does return #f, then the overall result is #f; otherwise, it’s #t.

If the given hash table is modified partway through this operation, the resulting behavior may be unpredictable.

procedure

(hash-v-each hash func)  void?

  hash : hash?
  func : (-> any/c any)
Iterates over the given hash table’s mapped values in an unspecified order and calls the given procedure on each one. Ignores the procedure’s results.

If the given hash table is modified partway through this operation, the resulting behavior may be unpredictable.