Database Reference
In-Depth Information
4.
This system will have three agents, each performing a different task. Here is the
function for the agent that converts all whole number ields to integer values.
It sends the output to another agent and uses that output as its own new value
so it can be validated:
(defn coerce-row [_ row sink]
(let [cast-row
(apply assoc row
(mapcat
(fn [k]
[k (try-read-string (k row))])
int-rows))]
(send sink conj cast-row)
cast-row))
5.
Here is the function for the agent that reads the input. It sends an item of the input
to the coerce-row agent, queues itself to read another item of the input, and sets
its value to the rest of the input:
(defn read-row [rows caster sink]
(when-let [[item & items] (seq rows)]
(send caster coerce-row item sink)
(send *agent* read-row caster sink)
items))
6.
Here is the validator for the coerce-row agent. It checks that the integer ields are
either integers or empty strings:
(defn int-val? [x] (or (int? x) (empty? x)))
(defn validate [row]
(or (nil? row)
(reduce #(and %1 (int-val? (%2 row)))
true int-rows)))
7. Finally, we'll deine a function that deines the agents, starts processing them, and
returns them:
(defn agent-ints [input-file]
(let [reader (agent (seque
(with-header
(lazy-read-csv
input-file))))
caster (agent nil)
sink (agent [])]
(set-validator! caster validate)
(send reader read-row caster sink)
{:reader reader
:caster caster
:sink sink}))
 
Search WWH ::




Custom Search