Created
May 3, 2015 10:59
-
-
Save cobalamin/e343de23cf45e48ad541 to your computer and use it in GitHub Desktop.
do-if macro in Clojure
This file contains 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
;; A small macro that enables if-else syntax without explicit do-blocks. | |
;; Example usage: | |
(comment | |
(do-if (= slang :british) | |
(println "oh bloody hell mate") | |
:posh-face | |
:else | |
(println "f*** dude") | |
:angry-face)) | |
(defmacro do-if [c & forms] | |
(let [else-idx (.indexOf forms :else) | |
[i-forms e-forms] (if (> else-idx -1) | |
[(take else-idx forms) (rest (drop else-idx forms))] | |
[forms nil])] | |
`(if ~c | |
(do ~@i-forms) | |
(do ~@e-forms)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment