OK, the second question I've sort of answered for myself, by riffing on the 
source of line-seq:

(defn expr-seq
  "Returns forms from src (assumed to be Clojure source) as a lazy sequence 
of expressions"
  [^java.io.PushbackReader src]
  (when-let [expr (read src)]
    (try
      (cons expr (lazy-seq (expr-seq src)))
      (catch RuntimeException eof))))

However, line-seq doesn't bother with catching an exception (presumably 
because it's not using a PushbackReader). So I come back to my first 
question: how do I detect the end of a file?

On Monday, 7 April 2014 18:45:44 UTC+1, Simon Brooke wrote:
>
> I've written a pair of functions which read a stream of Clojure source and 
> identify the var[*] definitions. They work, but the way they work seems 
> clumsy to me. Here they are:
>
> (defn find-vars-in-reader [eddi]
>       "Return a list of names of vars declared in the stream this reader 
> reads"
>       (try
>               (let [sexpr (read eddi)]
>                       (cond
>                               (nil? sexpr) nil
>                               (= (first sexpr) 'def) (cons (first (rest 
> sexpr)) (find-vars-in-reader eddi))
>                               true (find-vars-in-reader eddi)))
>               (catch RuntimeException eof)))
>
> (defn find-vars-in-file [filename]
>       "Return a list of names of vars declared in the file at this path name"
>       (with-open [eddi (java.io.PushbackReader. (reader filename))]
>               (find-vars-in-reader eddi)))
>
> The thing that really offends me about this is using catching a runtime 
> exception to stop reading. There must be a better way of detecting an 
> end-of-file, but I've missed it.
>
> The other thing, though, is I can't help feeling that it would be more 
> idiomatic Clojure to write a wrapper around a source file which allowed 
> functions to treat the file as a lazy sequence of S-expressions; and I can't 
> help feeling someone must already have done this. Have they? Is there a 
> library I should be looking at?
>
>

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to