Clojure 将Reduce迁移到Map

Clojure 将Reduce迁移到Map,clojure,Clojure,我的问题是如何使用map和doseq重新编写以下reduce解决方案?我在以下解决方案中遇到了很多问题 这个解决方案就是解决以下问题。具体来说,我有两个由clojure csv解析的csv文件。向量的每个向量都可以称为bene数据和gic数据。我想获取每行bene数据中一列中的值,并查看该值是否是gic数据中一行中的另一列。我想把gic数据中找不到的bene数据值累加到一个向量中。我最初试图累积到一个映射中,这是在调试打印时从堆栈溢出开始的。最后,我想获取这些数据,结合一些静态文本,并将其写入一

我的问题是如何使用map和doseq重新编写以下reduce解决方案?我在以下解决方案中遇到了很多问题

这个解决方案就是解决以下问题。具体来说,我有两个由clojure csv解析的csv文件。向量的每个向量都可以称为bene数据和gic数据。我想获取每行bene数据中一列中的值,并查看该值是否是gic数据中一行中的另一列。我想把gic数据中找不到的bene数据值累加到一个向量中。我最初试图累积到一个映射中,这是在调试打印时从堆栈溢出开始的。最后,我想获取这些数据,结合一些静态文本,并将其写入一个报告文件

以下功能:

(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时遇到一堵墙,因为我没有地方积累结果数据,除非我使用loop recur。

此解决方案将第2列的所有内容读取到一个集合中一次(因此,它是非惰性的),以便于编写。对于第1列的每个值,其性能也应优于重新扫描第2列。如果第2列太大而无法在内存中读取,请根据需要进行调整

(defn column
  "extract the values of a column out of a seq-of-seqs"
  [s-o-s n]
  (map #(nth % n) s-o-s))

(defn test-key-inclusion
  "return all values in column1 that arent' in column2"
  [column1 column2]
  (filter (complement (into #{} column2)) column1))

user> (def rows1 [[1 2 3] [4 5 6] [7 8 9]])
#'user/rows1

user> (def rows2 '[[a b c] [d 2 f] [g h i]])
#'user/rows2

user> (test-key-inclusion (column rows1 1) (column rows2 1))
(5 8)
也许
(defn测试键包含[column1 column2](删除(设置column2)column1))
?请随意