Skip to content

Instantly share code, notes, and snippets.

@hiredman
Created November 14, 2012 22:59
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 hiredman/4075459 to your computer and use it in GitHub Desktop.
Save hiredman/4075459 to your computer and use it in GitHub Desktop.
(require '[clojure.core.protocols :as p]
'[clojure.java.io :as io])
(deftype ByteSource [source]
p/CollReduce
(p/coll-reduce
[coll f]
(p/coll-reduce coll f nil))
(p/coll-reduce
[coll f init]
(with-open [x (io/input-stream source)]
(loop [n (.read x)
v init]
(if (= -1 n)
v
(let [r (f v n)]
(if (reduced? r)
@r
(recur (.read x) r))))))))
;; user=> (count (reduce conj [] (ByteSource. "/etc/passwd")))
;; (count (reduce conj [] (ByteSource. "/etc/passwd")))
;; 5086
;; user=>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment