May 2018
Beginner to intermediate
290 pages
6h 43m
English
To get a feeling for how clojure.spec works, let’s return to our book-store example and imagine that we’re writing code to process our familiar book maps, values that look like this:
| | {:title "Getting Clojure" :author "Olsen" :copies 1000000} |
Further, imagine that we’re getting our book data from a not-terribly-reliable source, and we’ve decided that the first thing we need to do is validate that this value that claims to be a book is in fact a book-shaped value. Clearly we could write a function:
| | (defn book? [x] |
| | (and |
| | (map? x) |
| | (string? (:author x)) |
| | (string? (:title x)) |
| | (pos-int? (:copies x)))) |
While this does work, the code it by hand approach to validating ...
Read now
Unlock full access