Database Reference
In-Depth Information
8.
If we run this, we get a map containing the agents. We can get the output data by
dereferencing the
:sink
agent:
user=> (def ags (agent-ints data-file))
#'user/ags
user=> (first @(:sink ags))
{:SUMLEV 160, :P035001 2056, :HU100.2000 3788, :HU100 4271, :NAME
"Abingdon town", :GEOID 5100148, :NECTA "", :CBSA "", :CSA "",
:P035001.2000 2091, :POP100.2000 7780, :CNECTA "", :POP100 8191,
:COUNTY "", :STATE 51}
How it works…
The
agent-ints
function is pretty busy. It deines the agents, sets everything up, and returns
the map containing the agents.
Let's break it down:
(let [reader (agent (seque
(with-header
(lazy-read-csv input-file))))
caster (agent nil)
sink (agent [])]
These lines deine the agents. One reads in the data, one converts it to integers, and one
accumulates the results. This igure illustrates that process:
Next,
read-row
simply gets the irst item of the input and sends it to the
caster
agent.
The
coerce-row
function tries to change the data in the columns listed in
int-rows
to
integers. It then passes the results to the
sink
agent. Before it's completely done, however,
its new state is passed to its validator function,
validate
.
The validator allows
nil
rows (for the agent's initial state) or integer ields that contain
either integers or empty strings. Finally, the
sink
agent is called with
conj
. It accumulates
the converted results.
See also
F
To learn how to use a nice DSL to validate data, see
Validating data with Valip
,
in
Chapter 2
,
Cleaning and Validating Data
.