3 ms·
(ns fourmls.core (:gen-class)) (defn csv-line-to-doubles [line] (map #(Double. %) (clojure.string/split line #","))) (defn barf-if-di
by dfboyd 9y ago
(ns fourmls.core
(:gen-class))
(defn csv-line-to-doubles
[line]
(map #(Double. %)
(clojure.string/split line #",")))
(defn barf-if-different-lengths
[s1 s2]
(if (not= (count s1)
(count s2))
(throw (Exception. "Mismatched lengths"))
s2))
(defn sum-csv-lines
[prev curr]
(if (nil? prev)
(csv-line-to-doubles curr)
(->> curr
csv-line-to-doubles
(barf-if-different-lengths prev)
(interleave prev)
(partition 2)
(map (partial apply +)))))
(defn -main
"I don't do a whole lot ... yet."
[filename]
(println
(clojure.string/join
","
(map str
(reduce sum-csv-lines
nil
(->> filename
clojure.java.io/reader
line-seq))))))
- joncampbelldev 9y agoVery nice. Perhaps using (partition 2 1 _) could be a clearer way to view the prev line instead of the reduce on a nil?