Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

transform-fn-bodies

  • Loading branch information...
commit d513481d939be246925ed7f91ce68cf937ac83fd 1 parent 3e989b5
Zach Tellman authored
1  src/potemkin.clj
@@ -19,6 +19,7 @@
19 19
20 20 (import-fn macros/unify-gensyms)
21 21 (import-fn macros/transform-defn-bodies)
  22 +(import-fn macros/transform-fn-bodies)
22 23
23 24
24 25
17 src/potemkin/macros.clj
@@ -32,7 +32,7 @@
32 32
33 33 (defn transform-defn-bodies
34 34 "Takes a (defn ...) form, and transform the bodies. The transform function is
35   - passed the arglist, the function metadata, and the function body."
  35 + passed the arglist and the function body."
36 36 [f form]
37 37 (let [form (macroexpand form)
38 38 fn-form (->> form (drop 2) first)
@@ -40,7 +40,6 @@
40 40 (second fn-form)
41 41 fn-form)
42 42 fn-form (macroexpand fn-form)
43   - fn-name (second form)
44 43 arity-forms (->> fn-form (drop-while symbol?))
45 44 arity-forms (map
46 45 (fn [arity-form]
@@ -50,3 +49,17 @@
50 49 `(~(first form)
51 50 ~(second form)
52 51 (fn* ~@arity-forms))))
  52 +
  53 +(defn transform-fn-bodies
  54 + "Takes a (fn ...) form, and transform the bodies. The transform function is
  55 + passed the arglist and the function body."
  56 + [f form]
  57 + (let [fn-form (macroexpand form)
  58 + arity-forms (->> fn-form (drop-while symbol?))
  59 + arity-forms (map
  60 + (fn [arity-form]
  61 + (let [args (first arity-form)]
  62 + `(~args ~@(f args (rest arity-form)))))
  63 + arity-forms)]
  64 + `(~@(take-while symbol? fn-form)
  65 + ~@arity-forms)))
26 test/potemkin/test/macros.clj
@@ -24,13 +24,31 @@
24 24
25 25 (declare foo)
26 26
27   -(defn test-transform [form & arities]
  27 +(defn test-transform-defn [form & arities]
28 28 (in-ns 'potemkin.test.macros)
29 29 (eval (transform-defn-bodies (constantly [1]) form))
30 30 (doseq [a arities]
31 31 (is (= 1 (apply foo (repeat a nil))))))
32 32
33 33 (deftest test-transform-defn-bodies
34   - (test-transform `(defn foo [x#]) 1)
35   - (test-transform `(defn foo []))
36   - (test-transform `(defn foo "doc-string" {:abc :def} ([]) ([x#]) ([x# y#]))))
  34 + (test-transform-defn `(defn foo [x#])
  35 + 1)
  36 + (test-transform-defn `(defn foo [])
  37 + 0)
  38 + (test-transform-defn `(defn foo "doc-string" {:abc :def} ([]) ([x#]) ([x# y#]))
  39 + 0 1 2))
  40 +
  41 +(defn test-transform-fn [form & arities]
  42 + (let [f (eval (transform-fn-bodies (constantly [1]) form))]
  43 + (doseq [a arities]
  44 + (is (= 1 (apply f (repeat a nil)))))))
  45 +
  46 +(deftest test-transform-fn-bodies
  47 + (test-transform-fn `(fn [x#])
  48 + 1)
  49 + (test-transform-fn `(fn foo [x#])
  50 + 1)
  51 + (test-transform-fn `(fn foo [])
  52 + 0)
  53 + (test-transform-fn `(fn foo ([]) ([x#]) ([x# y#]))
  54 + 0 1 2))

0 comments on commit d513481

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