Skip to content

Instantly share code, notes, and snippets.

@bsless
Created October 13, 2025 11:51
Show Gist options
  • Save bsless/c9319f4b7dacf6fdc8cc152cc0784cce to your computer and use it in GitHub Desktop.
Save bsless/c9319f4b7dacf6fdc8cc152cc0784cce to your computer and use it in GitHub Desktop.
Create a reducible over a file in JVM Clojure
(defn lines-reducible
[f]
(let [fr (java.io.FileReader. f)
br (java.io.BufferedReader. fr)]
(reify clojure.lang.IReduceInit
(reduce [_ rf init]
(loop [init init]
(let [line (java.io.BufferedReader/.readLine br)]
(if (nil? line)
init
(let [ret (rf init line)]
(if (reduced? ret)
@ret
(recur ret))))))))))
@bsless
Copy link
Author

bsless commented Oct 13, 2025

Probably the fastest / most idiomatic to consume lines fast in Clojure

@bsless
Copy link
Author

bsless commented Oct 13, 2025

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment