Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save LeifAndersen/0b3ef554f90568558ae95ea19e8b59aa to your computer and use it in GitHub Desktop.
Save LeifAndersen/0b3ef554f90568558ae95ea19e8b59aa to your computer and use it in GitHub Desktop.
#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