What's the easiest way to do something like this in Clojure?
require 'csv'
Dir["data/*.csv"].each do |file|
File.readlines(file).each do |line|
x, y, z = *CSV.parse_line(line)
# process this data
end
end
This is the shortest I've seen:
(require '[clojure.java.io :as io])
(filter #(.endsWith (.getName %) ".csv") (file-seq (io/file dir))))
From https://github.com/brentonashworth/one/blob/master/src/lib/clj/one/reload.clj#L28
Probably not the most concise possible, but perhaps something like the following?
(use 'clojure-csv.core)
(doseq [file (.listFiles (File. "data/"))]
(if (.matches (.getName file) ".*[.]CSV$")
(doseq [[x y z] (parse-csv (slurp file))]
"... do whatever stuff you want with x, y, z..."))))
N.B. uses the clojure-csv library.
This would be more elegant and shorter if I could find an obvious way to get a filtered directory list without resorting to regexes.... but it eludes me for the moment
UPDATE:
Brian's link to Java support for globbing is also useful and interesting and offers a more general/robust approach than simple regexes - however it depends on Java 1.7 (too cutting edge for some?) and it doesn't really shorten the code much.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With