May 2018
Beginner to intermediate
290 pages
6h 43m
English
So far we’ve been treating clojure.spec as a general-purpose library for ensuring that our data looks the way it should. And so it is. But think about how useful it would be during development to have a way to automatically do some spec matching at critical points in your code. For example, wouldn’t it be great if you could write a spec for the arguments of a function and have that spec checked each time the function is called?
We could do this by taking advantage of function pre and post conditions:
| | ;; Register a handy spec: An inventory is a collection of books. |
| | |
| | (s/def :inventory.core/inventory |
| | (s/coll-of ::book)) |
| | |
| | |
| | (defn find-by-title |
| | [title inventory] |
| | {:pre [(s/valid? ::title title) |
| | (s/valid? ::inventory ... |
Read now
Unlock full access