Skip to content

Instantly share code, notes, and snippets.

@wagjo
Created September 28, 2013 16:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wagjo/6743885 to your computer and use it in GitHub Desktop.
Save wagjo/6743885 to your computer and use it in GitHub Desktop.
reducible slurp
(require '[clojure.java.io :as jio])
(deftype Slurp [filename]
clojure.core.protocols/CollReduce
(coll-reduce [this f1]
(clojure.core.protocols/coll-reduce this f1 (f1)))
(coll-reduce [_ f1 init]
(with-open [#^java.io.Reader r
(apply jio/reader filename nil)]
(loop [ret init
c (.read r)]
(if (neg? c)
ret
(let [ret (f1 ret (char c))]
(if (reduced? ret)
@ret
(recur ret (.read r)))))))))
(defn slurp* [filename]
(Slurp. filename))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment