My question is how can I re-write the following reduce solution using map and probably doseq? I've been having a lot of trouble with the following solution.
That solution is to solve the following problem. Specifically, I have two csv files parsed by clojure-csv. Each vector of vectors could be called bene-data and gic-data. I want to take the value in a column in each row bene-data and see if that value is another column in one row in gic-data. I want to accumulate those bene-data values not found in gic-data into a vector. I originally tried to accumulate into a map, and that started off the stack overflow when trying to debug print. Eventually, I want to take this data, combine with some static text, and spit into a report file.
The following functions:
(defn is-a-in-b
"This is a helper function that takes a value, a column index, and a
returned clojure-csv row (vector), and checks to see if that value
is present. Returns value or nil if not present."
[cmp-val col-idx csv-row]
(let [csv-row-val (nth csv-row col-idx nil)]
(if (= cmp-val csv-row-val)
cmp-val
nil)))
(defn key-pres?
"Accepts a value, like an index, and output from clojure-csv, and looks
to see if the value is in the sequence at the index. Given clojure-csv
returns a vector of vectors, will loop around until and if the value
is found."
[cmp-val cmp-idx csv-data]
(reduce
(fn [ret-rc csv-row]
(let [temp-rc (is-a-in-b cmp-val cmp-idx csv-row)]
(if-not temp-rc
(conj ret-rc cmp-val))))
[]
csv-data))
(defn test-key-inclusion
"Accepts csv-data param and an index, a second csv-data param and an index,
and searches the second csv-data instances' rows (at index) to see if
the first file's data is located in the second csv-data instance."
[csv-data1 pkey-idx1 csv-data2 pkey-idx2 lnam-idx fnam-idx]
(reduce
(fn [out-log csv-row1]
(let [cmp-val (nth csv-row1 pkey-idx1 nil)
lnam (nth csv-row1 lnam-idx nil)
fnam (nth csv-row1 fnam-idx)
temp-rc (first (key-pres? cmp-val pkey-idx2 csv-data2))]
(println (vector temp-rc cmp-val lnam fnam))
(into out-log (vector temp-rc cmp-val lnam fnam))))
[]
csv-data1))
represent my attempt to solve this problem. I usually run into a wall trying to use doseq and map, because I have nowhere to accumulate the resulting data, unless I use loop recur.