-
-
Save ghadishayban/94ba17c2c177007f62ad to your computer and use it in GitHub Desktop.
Reducing over large files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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