I feel like I'm starting to be somewhat functional with Racket, but I'd
like to get more idiomatic.  I'd appreciate it if people would critique the
following code:


(define (data/fetch s key-list)
  (if (or (empty? s)
          (nor (list? s)
               (hash? s)))
      s
      (let* ([key (car key-list)]
             [val (if (hash? s)
                      (hash-ref s key)
                      (key s))])
        (data/fetch val (cdr key-list)))))

The intent is to take a nested data structure (hashes and lists only) and
walk down through it without needing a whole series of (hash-ref (car
(hash-ref...

Example:

-> d
'#hash(("foo" . "bar")
       (blag . #hash(("foo" . "bar")))
       (quux . (foo bar))
       (baz . 7))

-> (data/fetch d (list 'blag "foo"))
"bar"

-> (data/fetch d (list "no-such-key"))
; hash-ref: no value found for key
;   key: "no-such-key"
; [,bt for context]

-> L
'("foo" 7 (x y))

-> (data/fetch l (list first second))
"foo"

-> (data/fetch l (list third second))
'y

Thoughts?

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to