我的问题是如何使用 map 和可能的 doseq 重写以下 reduce 解决方案?我在使用以下解决方案时遇到了很多麻烦。
该解决方案是解决以下问题。具体来说,我有两个由 clojure-csv 解析的 csv 文件。每个向量的向量可以称为 bene-data 和 gic-data。我想获取每行 bene-data 中的一列中的值,并查看该值是否是 gic-data 中一行中的另一列。我想将 gic-data 中找不到的那些 bene-data 值累积到一个向量中。我最初尝试累积到地图中,并且在尝试调试打印时从堆栈溢出开始。最终,我想把这些数据,结合一些静态文本,然后吐到一个报告文件中。
以下功能:
(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))
代表我尝试解决这个问题。我通常在尝试使用 doseq 和 map 时碰壁,因为我无处可积累结果数据,除非我使用循环递归。