Once again a very (very) short one in... Clojure this time. As a matter of fact this is a short one before the longer one up to come :).
In order to challenge the code displayed in the upcoming post, I had again this problem of reading data from a file before converting it. Thanks to an astute reader in a previous post, I dropped the use of the clojure contrib 1.2 while using the 1.3 version of Clojure.
As I did not took the time to explore the new Clojure contributions I found my self a little bit upset while needing to load data from a file. The older Clojure contributions library hosted a suitable read-lines method in the clojure.contrib.io module. And I liked it.
Fortunately LISP languages always invite you in a nice way to develop your tools and build upon them. Of course I could not resist. At that point the do-not-reinvent-the-wheel conservationist's will have left the building.
Thank you to the others for staying :) The following harsh test renders what I needed:
(def from-test-file "/dev/projects/clojure/leinengen/ml/myfile.txt") (deftest read-lines-from-file-should-take-back-all (is (= 50 (count (read-lines from-test-file)))))
The read-lines takes as input the exact file location expression and returns a sequence (lazy if possible). Having explored lazy sequences in a previous post , I came to the working following solution:
(ns tools.file-reader (:import [java.io File BufferedInputStream BufferedReader FileReader])) (defn read-lines [from-file] (let [reader (BufferedReader. (FileReader. (File. from-file)))] (letfn [(flush-content [line] (lazy-seq (if line (cons line (flush-content (.readLine reader))) (.close reader))))] (flush-content (.readLine reader)))))
One will recognize the invocations of the BufferedReader, FileReader, and File java constructors recognizable by the appended "." at the end of the class name. The imports have been achieved through the use of the :import form at the top of the file.
The function body hosts a recursive inner function flush-content, in charge of
- taking the decision to stop the recursion when no more line has been found
- recur again using the readLine method from the BufferedReader.
And that's all folks. The big interesting part is in the next post.
Must write it now, so be seeing you !!! :)