Skip to content

Instantly share code, notes, and snippets.

@cobalamin
Created May 3, 2015 10:59
Show Gist options
  • Save cobalamin/e343de23cf45e48ad541 to your computer and use it in GitHub Desktop.
Save cobalamin/e343de23cf45e48ad541 to your computer and use it in GitHub Desktop.
do-if macro in Clojure
;; 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