[racket-users] Re: Code critique request: data/fetch, proc for walking hashes/lists

2016-01-19 Thread David K. Storrs

I apologize for the long ping time on this -- for whatever reason I didn't get 
any of the replies sent to my email and I thought no one had responded. 

JCG, Jack, thank you very much for taking the time. 

Jack: I had to stare at your code for a while before I got it, but thank you 
for it. That's really elegant, which is exactly the sort of thing I was looking 
for. Much appreciated. 

Dave

-- 
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.


[racket-users] Re: Code critique request: data/fetch, proc for walking hashes/lists

2016-01-11 Thread Jack Firth
I'd probably prefer using a fold over explicit recursion. This gets easier if 
you split your code into two steps - one that does just one level of lookup, 
and one that does nested lookups. Additionally, I'd prefer list indexes rather 
than pair lookup functions:

(define (data/ref s key)
  (cond
[(hash? s) (hash-ref s key)]
[(list? s) (list-ref s key)]
[else fail-somehow ...]))

(define (data/fetch s keys)
  (define (fetch-once key s)
(data/ref s key))
  (foldl fetch-once s keys))


> (data/ref (hash 'a 1 'b 2) 'a)
1
> (data/ref '(a b c) 2)
'c
> (data/fetch (hash 'a '(foo bar baz) 'b 0) '(a 2))
'baz

-- 
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.


[racket-users] Re: Code critique request: data/fetch, proc for walking hashes/lists

2016-01-11 Thread Jack Firth
I'd probably prefer using a fold over explicit recursion. This gets easier if 
you split your code into two steps - one that does just one level of lookup, 
and one that does nested lookups. Additionally, I'd prefer list indexes rather 
than pair lookup functions:

(define (data/ref s key)
  (cond
[(hash? s) (hash-ref s key)]
[(list? s) (list-ref s key)]
[else fail-somehow ...]))

(define (data/fetch s keys)
  (define (fetch-once key s)
(data/ref s key))
  (foldl fetch-once s keys))


> (data/ref (hash 'a 1 'b 2) 'a)
1
> (data/ref '(a b c) 2)
'c
> (data/fetch (hash 'a '(foo bar baz) 'b 0) '(a 2))
'baz

By splitting the recursion (define (data/ref s key)
  (cond
[(hash? s) (hash-ref s key)]
[(list? s) (list-ref s key)]
[else fail-somehow ...]))

(define (data/fetch s keys)
  (define (fetch-once key s)
(data/ref s key))
  (foldl fetch-once s keys))


> (data/ref (hash 'a 1 'b 2) 'a)
1
> (data/ref '(a b c) 2)
'c
> (data/fetch (hash 'a '(foo bar baz) 'b 0) '(a 2))
'baz

-- 
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.


[racket-users] Re: Code critique request: data/fetch, proc for walking hashes/lists

2016-01-10 Thread JCG
On Sunday, January 10, 2016 at 4:44:57 PM UTC-5, David K. Storrs wrote:
> 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)))

I'm not the one to answer about what is idiomatic Racket, but I'd probably be 
using (cond).

  (cond
[(hash? s)..]
[(pair? s)...]
[ else ...])

Also, you probably want a 4th parameter to hash-ref for failed lookup.

More than anything, thanks for using (nor).  I did not know of it beforehand.

JG




-- 
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.