Created
March 25, 2015 15:04
-
-
Save Metaxal/578b473bc48886f81123 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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