1
votes

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.

1

1 Answers

2
votes

This solution reads all of column 2 into a set once (so, it's non-lazy) for ease of writing. It should also perform better than re-scanning column 2 for each value of column 1. Adjust as needed if column 2 is too large to be read in memory.

(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)