Created
November 20, 2013 17:41
-
-
Save aamedina/7567601 to your computer and use it in GitHub Desktop.
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 simple-let | |
| [bindings & body] | |
| `((fn ~(->> (partition-all 2 bindings) (map first) (vec)) | |
| ~@body) | |
| ~@(->> (partition-all 2 bindings) (map second)))) | |
| (simple-let [x true] | |
| (= false x)) | |
| ;; returns false, because x is bound to true | |
| ;; what's cool is how the author points out that let is actually a type of lambda expression (anonymous function) | |
| ;; so this macro i wrote illustrates that explicitly by macroexpanding the let form into a lambda form | |
| ;; we can verify that this macro works correctly by macroexpanding the quoted form (we quote so that the reader | |
| ;; treats the form as a list of code, and not a list to be evaluated) | |
| (macroexpand | |
| '(simple-let [x true] | |
| (= false x))) | |
| ;; returns | |
| ((clojure.core/fn [x] (= false x)) true) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment