daily pastebin goal
41%
SHARE
TWEET

Untitled

a guest Mar 23rd, 2019 68 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #lang scratch
  2.  
  3. (define (better-keyword-apply func args)
  4.   (define-values (kws rest)
  5.     (let loop ([args args]
  6.                [kw '()]
  7.                [rest '()])
  8.       (cond
  9.         [(null? args) (values kw rest)]
  10.         [(keyword? (first args))
  11.          (loop (cddr args)
  12.                (cons (cons (first args) (second args)) kw)
  13.                rest)]
  14.         [else
  15.          (loop (cdr args)
  16.                kw
  17.                (cons (car args) rest))])))
  18.   (define sorted-kws (sort kws keyword<? #:key car))
  19.   (keyword-apply func
  20.                  (map car sorted-kws)
  21.                  (map cdr sorted-kws)
  22.                  (reverse rest)))
  23.  
  24. (module+ test
  25.   (define (f a #:x x #:y y [z "blue"])
  26.     (list a x y z))
  27.  
  28.   (better-keyword-apply f '(7 #:y 5 #:x 6))
  29.   (better-keyword-apply f '(#:x 5 7 #:y 12 6)))
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top