Last active
December 17, 2015 15:59
-
-
Save yuanmai/5635211 to your computer and use it in GitHub Desktop.
Turn let into defn
This file contains hidden or 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 defletn [name & body] | |
(let [args (->> body | |
last | |
second | |
(apply array-map) | |
keys | |
vec)] | |
`(defn ~name | |
~@(butlast body) | |
~args | |
~(nth (last body) 2)))) | |
(defletn foo | |
"Doc" | |
(for [n [1 2] | |
m [3 4]] | |
(+ m n))) | |
(defletn bar | |
(let [n 1 | |
m 2] | |
(+ m n))) |
This file contains hidden or 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
(defn complex-fn [] | |
(let [a 1] | |
(if (foo 2) | |
(let [b 2] | |
(bar b))))) | |
;; extract the inner let | |
(defletn baz | |
(let [b 2] | |
(bar b)) | |
(defn simpler-fn [] | |
(let [a 1] | |
(if (foo 2) | |
(baz 2)))) |
This file contains hidden or 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
user> (bar 5 6) | |
11 | |
user> (foo 5 6) | |
11 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment