Lazy characters input

/ Published in: Clojure
Save to your folder(s)

Taken from here, useful if you can not (or do not want to) line-seq file (e.g. when whole file is one big line).

Copy this code and paste it in your HTML
  1. (defn lazy-input
  2. "Returns a lazy sequence of characters from an input stream or Reader."
  3. [input-stream]
  4. (let [step (fn step []
  5. (let [c (.read input-stream)]
  6. (when-not (== c -1)
  7. (cons (char c) (lazy-seq (step))))))]
  8. (lazy-seq (step))))

Report this snippet


RSS Icon Subscribe to comments

You need to login to post a comment.