Skip to content

Instantly share code, notes, and snippets.

@agermanidis
Created October 18, 2011 21:59
Show Gist options
  • Save agermanidis/1296854 to your computer and use it in GitHub Desktop.
Save agermanidis/1296854 to your computer and use it in GitHub Desktop.
(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