-
-
Save mattdeboard/f057008587243ea585ad to your computer and use it in GitHub Desktop.
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
(defn pdf? [d] | |
(= (type d) Document)) | |
(defmacro with-pdf [bindings & body] | |
(#'clojure.core/assert-args | |
(vector? bindings) "A vector for its binding" | |
(even? (count bindings)) "an even number of forms in binding vector") | |
(cond | |
(= (count bindings) 0) `(do ~@body) | |
(symbol? (bindings 0)) `(if (pdf? ~(bindings 1)) | |
(let ~(vector (bindings 0) | |
`(doto ~(bindings 1) (.open))) | |
(try (with-pdf ~(subvec bindings 2) ~@body) | |
(finally | |
(. ~(bindings 0) close)))) | |
(throw (IllegalArgumentException. | |
"bind target must be a Document instance"))) | |
:else (throw (IllegalArgumentException. | |
"with-pdf only allows binding of Documents to Symbols")))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment