Skip to content

Instantly share code, notes, and snippets.

@mattdeboard
Created July 26, 2012 23:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattdeboard/f057008587243ea585ad to your computer and use it in GitHub Desktop.
Save mattdeboard/f057008587243ea585ad to your computer and use it in GitHub Desktop.
(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