nested-hash
nested-hash-ref
nested-hash-set
nested-hash-ref*
nested-hash-set*
8.12

nested-hash🔗ℹ

Kevin R. Stravers

 (require nested-hash) package: nested-hash

Provides hash-ref and hash-set variants that operate on nested hash tables, that is, a hash table inside a hash table.

procedure

(nested-hash-ref hash*    
  key ...+    
  [#:default default])  any?
  hash* : hash?
  key : any/c
  default : any/c = #f
Accesses a hash table recursively using the given keys. default is returned if a key does not exist. An error is raised if an access is performed on a non-hash entry.

procedure

(nested-hash-set hash*    
  key ...+    
  value    
  [#:hash hash])  any?
  hash* : hash?
  key : any/c
  value : any/c
  hash : any/c = hash
Functionally edits a hash table using the given keys and value. Non-existent keys will automatically become new subtables. Existing intermediate keys that are associated with non-hash values will raise an error. The #:hash keyword specifies a constructor for creating new nested hash tables.

Examples:
> (nested-hash-ref (hash 'a (hash 'b 123)) 'a 'b)

123

> (nested-hash-set (hash) 'a 'b 123)

'#hash((a . #hash((b . 123))))

Similarly to the previous functions we have similar functions that take actual lists instead of inline-lists. These may be useful to avoid using apply when generating lists of accesses.

procedure

(nested-hash-ref* hash*    
  keys    
  [#:default default])  any?
  hash* : hash?
  keys : (listof any/c)
  default : any/c = #f
Accesses a hash table recursively using the given keys. default is returned if a key does not exist. An error is raised if an access is performed on a non-hash entry.

procedure

(nested-hash-set* hash*    
  keys    
  value    
  [#:hash hash])  any?
  hash* : hash?
  keys : (listof any/c)
  value : any/c
  hash : any/c = hash
Functionally edits a hash table using the given keys and value. Non-existent keys will automatically become new subtables. Existing intermediate keys that are associated with non-hash values will raise an error. The #:hash keyword specifies a constructor for creating new nested hash tables.

Examples:
> (nested-hash-ref* (hash 'a (hash 'b 123)) '(a b))

123

> (nested-hash-set* (hash) '(a b) 123)

'#hash((a . #hash((b . 123))))