Skip to content
Browse files

transform-fn-bodies

  • Loading branch information...
1 parent 3e989b5 commit d513481d939be246925ed7f91ce68cf937ac83fd @ztellman committed Mar 2, 2012
Showing with 38 additions and 6 deletions.
  1. +1 −0 src/potemkin.clj
  2. +15 −2 src/potemkin/macros.clj
  3. +22 −4 test/potemkin/test/macros.clj
View
1 src/potemkin.clj
@@ -19,6 +19,7 @@
(import-fn macros/unify-gensyms)
(import-fn macros/transform-defn-bodies)
+(import-fn macros/transform-fn-bodies)
View
17 src/potemkin/macros.clj
@@ -32,15 +32,14 @@
(defn transform-defn-bodies
"Takes a (defn ...) form, and transform the bodies. The transform function is
- passed the arglist, the function metadata, and the function body."
+ passed the arglist and the function body."
[f form]
(let [form (macroexpand form)
fn-form (->> form (drop 2) first)
fn-form (if (= ".withMeta" (str (first fn-form)))
(second fn-form)
fn-form)
fn-form (macroexpand fn-form)
- fn-name (second form)
arity-forms (->> fn-form (drop-while symbol?))
arity-forms (map
(fn [arity-form]
@@ -50,3 +49,17 @@
`(~(first form)
~(second form)
(fn* ~@arity-forms))))
+
+(defn transform-fn-bodies
+ "Takes a (fn ...) form, and transform the bodies. The transform function is
+ passed the arglist and the function body."
+ [f form]
+ (let [fn-form (macroexpand form)
+ arity-forms (->> fn-form (drop-while symbol?))
+ arity-forms (map
+ (fn [arity-form]
+ (let [args (first arity-form)]
+ `(~args ~@(f args (rest arity-form)))))
+ arity-forms)]
+ `(~@(take-while symbol? fn-form)
+ ~@arity-forms)))
View
26 test/potemkin/test/macros.clj
@@ -24,13 +24,31 @@
(declare foo)
-(defn test-transform [form & arities]
+(defn test-transform-defn [form & arities]
(in-ns 'potemkin.test.macros)
(eval (transform-defn-bodies (constantly [1]) form))
(doseq [a arities]
(is (= 1 (apply foo (repeat a nil))))))
(deftest test-transform-defn-bodies
- (test-transform `(defn foo [x#]) 1)
- (test-transform `(defn foo []))
- (test-transform `(defn foo "doc-string" {:abc :def} ([]) ([x#]) ([x# y#]))))
+ (test-transform-defn `(defn foo [x#])
+ 1)
+ (test-transform-defn `(defn foo [])
+ 0)
+ (test-transform-defn `(defn foo "doc-string" {:abc :def} ([]) ([x#]) ([x# y#]))
+ 0 1 2))
+
+(defn test-transform-fn [form & arities]
+ (let [f (eval (transform-fn-bodies (constantly [1]) form))]
+ (doseq [a arities]
+ (is (= 1 (apply f (repeat a nil)))))))
+
+(deftest test-transform-fn-bodies
+ (test-transform-fn `(fn [x#])
+ 1)
+ (test-transform-fn `(fn foo [x#])
+ 1)
+ (test-transform-fn `(fn foo [])
+ 0)
+ (test-transform-fn `(fn foo ([]) ([x#]) ([x# y#]))
+ 0 1 2))

0 comments on commit d513481

Please sign in to comment.
Something went wrong with that request. Please try again.