Skip to content

Instantly share code, notes, and snippets.

@wildermuthn
Last active November 22, 2015 08:52
Show Gist options
  • Save wildermuthn/629ec86a1f4ce421eff2 to your computer and use it in GitHub Desktop.
Save wildermuthn/629ec86a1f4ce421eff2 to your computer and use it in GitHub Desktop.
Define locally scoped functions with fewer parens
(defn anon-fn
"Should just use whatever #() uses. Limited at this point"
[body]
(let [arg1 (gensym)
arg2 (gensym)
body (cw/postwalk-replace {(symbol "%") arg1
(symbol "%1") arg1
(symbol "%2") arg2} body)]
`(fn [& [~arg1 ~arg2]] ~body)))
(defn anon-pair [pair]
(let [f-symbol (first pair)
f-fn (last pair)
f-anon (anon-fn f-fn)]
(vector f-symbol f-anon)))
(defmacro let# [bindings body]
(let [pairs (partition 2 bindings)
f-bindings (into [] (mapcat anon-pair pairs))]
`(let ~f-bindings ~body)))
(comment
(let# [life (+ 4 %)
universe (* % 10)
everything (-> % inc inc)]
(-> 0 life universe everything)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment