Database Reference
In-Depth Information
Maintaining data consistency with
validators
Clojure has a number of tools to work with agents. One of them is validators. When an agent's
message function returns a value, any validator functions assigned to that agent receive the
agent's data before it does. If the validators return true, all is well. The agent is updated and
processing continues. However, if any validator returns false or raises an error, an error is
raised on the agent.
This can be a handy tool to make sure that the data assigned to your agent conforms to your
expectations, and it can be an important check on the consistency and validity of your data.
For this recipe, we'll read data from a CSV ile and convert the values in some of the columns
to integers. We'll use a validator to ensure that this actually happens.
Getting ready
For this recipe, we'll use the dependencies and requirements that we did from the Managing
program complexity with STM recipe. We'll also use the lazy-read-csv and with-header
functions from that recipe, and we'll use the data ile that we used in that recipe. We'll
keep that ilename bound to data-file .
How to do it…
This recipe will be built from a number of shorter functions:
1. Let's deine a list of the rows that will need to be converted to integers. Looking at
the data ile, we can come up with this:
(def int-rows
[:GEOID :SUMLEV :STATE :POP100 :HU100 :POP100.2000
:HU100.2000 :P035001 :P035001.2000])
2. Now, we'll deine a predicate function to check whether a value is an integer or not:
(defn int? [x]
(or (instance? Integer x) (instance? Long x)))
3.
We'll create a function that attempts to read a string to an integer, but silently returns
the original value if there's an exception:
(defn try-read-string [x]
(try
(read-string x)
(catch Exception ex
x)))
 
Search WWH ::




Custom Search