mark.warren
2018-11-23 13:12:00
I just ‘know’ there is an easier way than this to traverse down a bunch of nested hash tables using a list of keys but I’m darned if I can think of it, any ideas? (define (descend hsh lst)
(cond [(empty? lst) '()]
[(empty? (rest lst)) (hash-ref hsh (first lst) '())]
[else (descend (hash-ref hsh (first lst)) (rest lst))]))
d_run
2018-11-23 15:29:03
nested-hash
pretty much does the same thing https://github.com/BourgondAries/nested-hash/blob/master/main.rkt
d_run
2018-11-23 15:30:53
or use a trick from Fear of Macros
d_run
2018-11-23 15:30:56
(define (hash-refs ks h [def #f])
(with-handlers ([exn:fail? (const (cond [(procedure? def) (def)]
[else def]))])
(for/fold ([h h])
([k (in-list ks)])
(hash-ref h k))))
d_run
2018-11-23 15:31:24
;; example:
;; (hash-refs '(a b c) (hash 'a (hash 'b (hash 'c 2)))) -> 2
;; (hash-refs '(a d) (hash 'a 2) 10) -> 10
mark.warren
2018-11-23 15:31:57
@d_run Thanks, I wasn’t too far off then. Cheers for the pointers.
d_run
2018-11-23 15:32:40
If you want super powered ways to slice and dice nested data structures lens
(https://docs.racket-lang.org/lens/index.html?q=lens) is very nice
mark.warren
2018-11-23 15:33:11
Cool