Created
October 18, 2011 21:59
-
-
Save agermanidis/1296854 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
(defmacro defmixfix [name args & body] | |
(let [kws (vec (filter keyword? args)) | |
filter-indices (fn [pred coll] | |
(filter (complement nil?) (map-indexed #(if (pred %2) %1 nil) coll))) | |
positions (vec (filter-indices keyword? args)) | |
genkwsym (fn [kw] (with-meta (gensym) {:name kw})) | |
kw-symbols (reduce #(assoc %1 %2 (genkwsym %2)) {} kws) | |
args-with-symbols (vec (map #(if (keyword? %) (% kw-symbols) %) args))] | |
`(defn ~name ~args-with-symbols | |
(do | |
(assert (= ~kws ((comp reverse vals) (select-keys ~args-with-symbols ~positions)))) | |
~@body)))) | |
(comment | |
(defmixfix ifm [e :then t :else f] | |
(if e t f)) | |
(ifm true :then 1 :else 0) | |
(ifm true :asdf 1 :qwerty 0) ; throws AssertionError | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment