[racket] Apply proc with hasheq

From: Danny Yoo (dyoo at hashcollision.org)
Date: Tue Dec 18 02:05:49 EST 2012

>   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")?


Would keyword-apply work for you?
http://docs.racket-lang.org/reference/procedures.html#(def._((lib._racket/private/base..rkt)._keyword-apply))

To apply in your situation, perhaps something like this:

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

#lang racket

(define (hash->keyword-apply f a-hash [rest-args '()])
  (define-values (keys vals)
    (for/fold ([keys '()] [vals '()])
              ([(k v) (in-hash a-hash)])
      (values (cons (string->keyword (symbol->string k)) keys)
              (cons v vals))))
  (keyword-apply f keys vals rest-args))


(define (f #:x x #:y y)
  (list x y))

(hash->keyword-apply f #hash((x . 3) (y . 4)))

Posted on the users mailing list.