Skip to content

Instantly share code, notes, and snippets.

@yuanmai
Last active December 17, 2015 15:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yuanmai/5635211 to your computer and use it in GitHub Desktop.
Save yuanmai/5635211 to your computer and use it in GitHub Desktop.
Turn let into defn
(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)))
(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))))
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