[racket] Apply proc with hasheq

From: John Clements (clements at brinckerhoff.org)
Date: Tue Dec 18 02:02:50 EST 2012

On Dec 17, 2012, at 10:51 PM, Haiwei Zhou wrote:

> Hi,
> 
>   I am writing some code with json. Request may look likes (hasheq 'action "doit" 'arg1 "a" 'arg2 "b"). 
> 
>   Is there any function or library which can apply a proc with hasheq? The expected result is (proc #:action "doit" #:arg1 "a" #:args "b")?

I think the easiest way to do this would be to use "match" inside the body of the procedure being defined. So:

(define (hashy-proc ht)
  (match ht
    [(hash-table ("action" a) ("arg1" a1) ("arg2" a2))
      ... code that uses a and a1 and a2 ...])


If this is too verbose for you, you could hide it using a macro, so that it looks like

(define-hashproc (proc #:action a #:arg1 a1 #:arg2)
  ... code that uses a and a1 and a2...)


... but I would try it first with the first form, to see whether you can live with it.


That's my thought, anyhow.

John Clements

-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 4370 bytes
Desc: not available
URL: <http://lists.racket-lang.org/users/archive/attachments/20121217/90cb39e7/attachment.p7s>

Posted on the users mailing list.