Skip to content

Instantly share code, notes, and snippets.

@cjlarose
Last active October 21, 2015 00:08
Show Gist options
  • Save cjlarose/bf9f5004f18098fbe358 to your computer and use it in GitHub Desktop.
Save cjlarose/bf9f5004f18098fbe358 to your computer and use it in GitHub Desktop.
Lazy byte sequence in Clojure given something to read from (like stdin)
;; 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