Skip to content

Instantly share code, notes, and snippets.

@Metaxal
Created March 25, 2015 15:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Metaxal/578b473bc48886f81123 to your computer and use it in GitHub Desktop.
Save Metaxal/578b473bc48886f81123 to your computer and use it in GitHub Desktop.
#lang racket
;; proc : procedure? ; procedure to apply
;; dict : dict? ; dictionary of keywords and values
;; A key can be either a keyword or a symbol that is turned into a keyword.
;; largs : list? ; positional arguments
;; Returns the result of the application of proc to the positional arguments and the keyword values.
(define (keyword-apply/dict proc dict largs)
(define alist
(sort
(for/list ([(k v) (in-dict dict)])
(cons
(cond [(keyword? k) k]
[(symbol? k) (string->keyword (symbol->string k))]
[else (error "Not a keyword or symbol:" k)])
v))
keyword<? #:key car))
(keyword-apply proc (map car alist) (map cdr alist) largs))
(define (f a b #:c c #:d d)
(list a b c d))
(keyword-apply/dict f '((c . 3) (#:d . 4)) '(1 2)) ; -> '(1 2 3 4)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment