Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Reducing over large files
(defn line-reducible
"Returns a reducible collection over stream of lines from a BufferedReader"
[^BufferedReader r]
(reify
clojure.lang.IReduceInit
(reduce [_ f init]
(with-open [rdr r]
(loop [line (.readLine rdr) acc init]
(if (reduced? acc)
@acc
(if line
(recur (.readLine rdr)
(f acc line))
acc)))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment