I had to process each line of a very large file, 120MB,
and did not want to read in the whole file at once. I
wrote this function, chunk-file, that allows me to pass
in a function and args that will process each line.
It works great for me, but hoping to get any feedback
about coding style or there is already something out
there that does such a thing or whatever. Thanks.
(defn chunk-file
"Takes a file, number of lines, a function and args.
Reads in line-size from the file and passes each line
and the args to the given function."
([file line-size f & args]
(with-open r file
(loop [l (.readLine r)
tlines []]
(let [end? (nil? l)
lines (if (not end?) (conj tlines l) tlines)
chunk? (zero? (rem (count lines) line-size))]
(if (or chunk? end?)
(do
(doseq line lines (apply f line args))
(if (not end?)
(recur (.readLine r) [])))
(recur (.readLine r) lines)))))))
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To post to this group, send email to [email protected]
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
-~----------~----~----~----~------~----~------~--~---