Skip to content

Instantly share code, notes, and snippets.

@pervognsen
Created March 9, 2010 14:11
Show Gist options
  • Save pervognsen/326606 to your computer and use it in GitHub Desktop.
Save pervognsen/326606 to your computer and use it in GitHub Desktop.
;; improved version from chouser
(defmacro named [kwargs defaults & body]
`(let [{:keys [~@(take-nth 2 defaults)] :or ~(apply array-map defaults)}
(apply array-map ~kwargs)]
~@body))
;; Example
(defn draw [x & args]
(named args [size 42
color :blue]
(print "draw" size color)))
(draw []) ;; => draw 42 :blue
(draw [] :size 666) ;; => draw 666 :blue
(draw [] :color :red) ;; => draw 42 :red
(draw [] :size 666 :color :red) ;; => draw 666 :red
(draw [] :color :red :size 666) ;; => draw 666 :red
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment