On 17 January 2011 13:48, John Svazic <[email protected]> wrote:
> Benny already answered, but here's the common block that I'm using for
> my Clojure submissions:
>
> (import '(java.io BufferedReader FileReader))
> (defn process-file [file-name]
> (let [rdr (BufferedReader. (FileReader. file-name))]
> (line-seq rdr)))
>
> (defn my-func [col]
> ; Do something interesting
> )
>
> (println (my-func (process-file (first *command-line-args*))))
>
> Basically I read the lines of the file into a sequence, then process
> that sequence in my function. Since I'm only dealing with the first
> parameter passed to my script, a (first *command-line-args*) call is
> equivalent to args[0] in other languages like Java.
>
If you include clojure.contrib.io, you could use read-lines:
(defn read-lines
"Like clojure.core/line-seq but opens f with reader. Automatically
closes the reader AFTER YOU CONSUME THE ENTIRE SEQUENCE."
[f]
(let [read-line (fn this [^BufferedReader rdr]
(lazy-seq
(if-let [line (.readLine rdr)]
(cons line (this rdr))
(.close rdr))))]
(read-line (reader f))))
Regards,
Stuart
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en