Posted By

ef on 01/23/12


Tagged

characters lazy


Versions (?)

Lazy characters input


 / Published in: Clojure
 

Taken from here http://groups.google.com/group/clojure/msg/1590311bac972d15, useful if you can not (or do not want to) line-seq file (e.g. when whole file is one big line).

  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  

You need to login to post a comment.