Skip to content

Instantly share code, notes, and snippets.

@valpackett
Last active December 14, 2015 15:49
Show Gist options
  • Save valpackett/5111105 to your computer and use it in GitHub Desktop.
Save valpackett/5111105 to your computer and use it in GitHub Desktop.
Clojure macro for executing code if a library exists
(defmacro when-require [n & body]
(let [nn (eval n)]
(try (require nn)
(catch Throwable e nil))
(when (find-ns nn)
`(do ~@body))))
(defmacro if-require [n body1 body2]
(let [nn (eval n)]
(try (require nn)
(catch Throwable e nil))
(if (find-ns nn)
`(do ~body1)
`(do ~body2))))
; Usage example:
;
; (when-require 'immutant.messaging
; (immutant.messaging/start "topic.whatever")
; (immutant.messaging/publish "topic.whatever" "hi"))
;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment