Monday, 28 February 2011

Clojure

In collaboration with Mr Yannick Malins as a result of having too much time in the evenings of a ski holiday. Not actual Scala but close enough.

Recurses thru a directory looking for 'test.txt' files and cats the result to the output file and stdout.


(import
'(java.io File FileWriter BufferedReader FileReader))

(def myfile (new File "C:\temp\source\"))
(def myfile2 (new File "C:\temp\out.txt"))

(defn recurse-files [file]
(if (.isDirectory file)
(apply str (map recurse-files (.listFiles file)))
(when (= (.getName file) "test.txt")
(let [reader (new BufferedReader (new FileReader file))]
(apply str (map
#(str (.getParent file) "/" % \newline)
(line-seq reader)))))))

(doto (new FileWriter myfile2)
(.write (recurse-files myfile))
(.close))

(recurse-files myfile)

No comments:

Post a comment