Skip to content

Instantly share code, notes, and snippets.

@ghadishayban
Last active December 17, 2015 05:09
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ghadishayban/94ba17c2c177007f62ad to your computer and use it in GitHub Desktop.
Save ghadishayban/94ba17c2c177007f62ad to your computer and use it in GitHub Desktop.
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