Last active
October 21, 2015 00:08
-
-
Save cjlarose/bf9f5004f18098fbe358 to your computer and use it in GitHub Desktop.
Lazy byte sequence in Clojure given something to read from (like stdin)
This file contains hidden or 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
;; Similar to https://clojuredocs.org/clojure.core/line-seq, | |
;; but is byte-oriented instead of line-oriented | |
(defn byte-seq [^java.io.BufferedReader rdr] | |
(lazy-seq | |
(let [ch (.read rdr)] | |
(if (= ch -1) | |
'() | |
(cons ch (byte-seq rdr)))))) | |
;; For example, to create a lazy sequence of bytes from stdin, | |
;; You might use the following: | |
(import '(java.io BufferedReader)) | |
(byte-seq (BufferedReader. *in*)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment