开发者

What is the most concise Clojure equivalent for Ruby's Dir.glob()?

What's the easiest way to do something like this in Clojure?

require 'csv'
Dir["data/*.csv"].each do |file|
  File.readlines(开发者_StackOverflow社区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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜