Created
September 28, 2013 16:41
-
-
Save wagjo/6743885 to your computer and use it in GitHub Desktop.
reducible slurp
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
(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