From 6b6395db103209ff66f0da2bd3920c326e140104 Mon Sep 17 00:00:00 2001 From: Mauro Lopes Date: Thu, 7 Aug 2014 02:34:25 -0300 Subject: [PATCH] Make defnp support multi-arity functions (@maurolopes) --- src/taoensso/timbre/profiling.clj | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/src/taoensso/timbre/profiling.clj b/src/taoensso/timbre/profiling.clj index 658ccda..10a479e 100644 --- a/src/taoensso/timbre/profiling.clj +++ b/src/taoensso/timbre/profiling.clj @@ -218,12 +218,17 @@ (defmacro defnp "Like `defn` but wraps body in `p` macro." {:arglists '([name ?doc-string ?attr-map [params] ?prepost-map body])} [name & sigs] - (let [[name [params & sigs]] (encore/name-with-attrs name sigs) - prepost-map (when (and (map? (first sigs)) (next sigs)) (first sigs)) - body (if prepost-map (next sigs) sigs)] - `(defn ~name ~params ~(or prepost-map {}) - (pspy ~(clojure.core/name name) - ~@body)))) + (let [[name sigs] (encore/name-with-attrs name sigs) + [sigs func->str] (if (vector? (first sigs)) ;only one arity + [(list sigs) (fn [name _] (clojure.core/name name))] + [sigs (fn [name params] (str (clojure.core/name name) \_ (count params)))]) + new-sigs (map (fn [[params & others]] + (let [[prepost-map & body] (if (and (map? (first others)) (next others)) ;has prepost map + others + (cons {} others))] + `(~params ~prepost-map (pspy ~(func->str name params) ~@body)))) + sigs)] + `(defn ~name ~@new-sigs))) (comment (defnp foo "Docstring "[x] "boo" (* x x)) (macroexpand '(defnp foo "Docstring" [x] "boo" (* x x)))