hash-star
hash-ref*
hash-keys-set*
8.12

hash-star🔗ℹ

Lucas Sta Maria <lucas@priime.dev>

 (require hash-star) package: hash-star

This package provides additional functions for interacting with hash tables, specifically nested hash tables.

procedure

(hash-ref* ht key ... [#:else default])  any

  ht : hash?
  key : any/c
  default : (or/c procedure? #f) = #f
Traverses through the given hash table "ht" with the "key"s in order. If at any point, a key isn’t contained in a traversed hash table, or traversal cannot progress because a value wasn’t a hash table, the function will error. A default value or substitute error can replace the default error by passing in a lambda into the "#:else" keyword.

Examples:
> (hash-ref* (hash 'a 5))

'#hash((a . 5))

> (hash-ref* (hash 'a 5) 'a)

5

> (hash-ref* (hash 'a (hash 'b 6)) 'a)

'#hash((b . 6))

> (hash-ref* (hash 'a (hash 'b 6)) 'a 'b)

6

> (hash-ref* (hash 'a (hash 'b 6)) 'a 'b 'c)

hash-ref*: no value found for key 'c that is 2 layers deep

> (hash-ref* (hash 'a (hash 'b 6))
             'a 'b 'c
             #:else (thunk #f))

#f

procedure

(hash-keys-set* ht key ... val)  hash?

  ht : hash?
  key : any/c
  val : any/c
Traverses through the given hash table "ht" with the "key"s in order, creating new nested hash tables if necessary, and setting the last key to the given value.

Examples:
> (hash-keys-set* (hash) 'a 5)

'#hash((a . 5))

> (hash-keys-set* (hash 'a 5) 'a 6)

'#hash((a . 6))

> (hash-keys-set* (hash 'a (hash 'b 5)) 'a 'b 6)

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

> (hash-keys-set* (hash 'a (hash 'b 5)) 'a 'c 8)

'#hash((a . #hash((b . 5) (c . 8))))

> (hash-keys-set* (hash) 'a 'b 'c 'd)

'#hash((a . #hash((b . #hash((c . d))))))