Created
April 20, 2016 12:55
-
-
Save anonymous/2ab4b4adedc1c35881a96b8871e9f7ab 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 fnp | |
"A macro for defining functions that take an optional first argument that is a map of parameters" | |
[& body] | |
(let [[fn-name & fn-body] (if (symbol? (first body)) | |
body | |
(cons (gensym "fnp") body)) | |
fn (if-not (->> body flatten (some #(= :- %))) | |
'fn | |
'schema/fn)] | |
`(let [fnp# (~fn ~@body)] | |
(fn ~fn-name | |
[& args#] | |
(if (not (map? (first args#))) | |
;; If the first argument isn't a map, make it an empty map | |
(apply fnp# {} args#) | |
(apply fnp# args#)))))) | |
(defmacro defnp | |
"A macro for defining functions that take an optional first argument, that is a map of parameters" | |
[fn-name & body] | |
(let [metadata (take-while | |
#(not (or (vector? %) (and (list? %) (-> % first vector?)))) | |
body) | |
fn-body (drop (count metadata) body)] | |
`(def ~fn-name | |
~@metadata | |
(with-meta | |
(fnp ~fn-name ~@fn-body) | |
(meta ~fn-name))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment