O'Reilly logo

Clojure Programming by Brian Carper, Christophe Grand, Chas Emerick

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Relying upon Assertions

Our HTML generation functions are decent, but someone somewhere will inevitably attempt to abuse the API. For example, the Clojure-standard representation of XML (as produced by clojure.xml) looks like this:

{:tag :a, :attrs {:href "http://clojure.org"}, :content ["Clojure"]}

That’s quite different than our vector-based HTML representation. Unsurprisingly, html will do odd things with this map, since it’s not at all what it’s expecting:

(html {:tag :a, :attrs {:href "http://clojure.org"}, :content ["Clojure"]})
;= "{:content [\"Clojure\"], :attrs {:href \"http://clojure.org\"}, :tag :a}"

Cripes, it returns a string! That’s appropriate insofar as we somewhat lazily use html recursively to process the contents of element vectors, but this particular result is clearly not useful if we imagine a user that (perhaps reasonably) thinks she might be able to serialize a clojure.xml data structure to an HTML string using our html function. This is a situation where we’d like to fail as quickly as possible.

Enter assertions. Assertions test a condition and throw an error if the condition is not true:

(defn attrs
  [attrs]
  (assert (or (map? attr-map)                                    1 (nil? attr-map)) "attr-map must be nil, or a map") (->> attrs (mapcat (fn [[k v]] [\space (name k) "=\"" v "\""])) (apply str))) (attrs "hi") ;= #<AssertionError java.lang.AssertionError: ;= Assert failed: attr-map must be nil, or ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required