Created
March 18, 2019 23:40
-
-
Save LeifAndersen/0b3ef554f90568558ae95ea19e8b59aa to your computer and use it in GitHub Desktop.
This file contains hidden or 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 scratch | |
(define (better-keyword-apply func args) | |
(define-values (kws rest) | |
(let loop ([args args] | |
[kw '()] | |
[rest '()]) | |
(cond | |
[(null? args) (values kw rest)] | |
[(keyword? (first args)) | |
(loop (cddr args) | |
(cons (cons (first args) (second args)) kw) | |
rest)] | |
[else | |
(loop (cdr args) | |
kw | |
(cons (car args) rest))]))) | |
(define sorted-kws (sort kws keyword<? #:key car)) | |
(keyword-apply func | |
(map car sorted-kws) | |
(map cdr sorted-kws) | |
(reverse rest))) | |
(module+ test | |
(define (f a #:x x #:y y [z "blue"]) | |
(list a x y z)) | |
(better-keyword-apply f '(7 #:y 5 #:x 6)) | |
(better-keyword-apply f '(#:x 5 7 #:y 12 6))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment