Re: How do I detect the end of a file without catching an exception?
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.
Re: How do I detect the end of a file without catching an exception?
On Mon 7 Apr 2014 at 11:07:37AM -0700, Simon Brooke wrote: 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? Use clojure.core/read with three params: [stream eof-error? eof-value] From the Slamhound source: (take-while #(not= ::done %) (repeatedly #(read rdr false ::done))) guns pgpOWqGF7SNcJ.pgp Description: PGP signature
Re: How do I detect the end of a file without catching an exception?
On Monday, April 7, 2014 1:14:40 PM UTC-5, guns wrote: On Mon 7 Apr 2014 at 11:07:37AM -0700, Simon Brooke wrote: 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? Use clojure.core/read with three params: [stream eof-error? eof-value] From the Slamhound source: (take-while #(not= ::done %) (repeatedly #(read rdr false ::done))) guns If using clojure.edn, which might be a good idea here, the syntax is slightly different for specifying an EOF marker, e.g. (let [sentinel (Object.)] (take-while #(not= sentinel %) (repeatedly #(edn/read {:eof sentinel} file -- 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.
Re: How do I detect the end of a file without catching an exception?
Thank you, that's neat! On Monday, 7 April 2014 19:14:40 UTC+1, guns wrote: On Mon 7 Apr 2014 at 11:07:37AM -0700, Simon Brooke wrote: 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? Use clojure.core/read with three params: [stream eof-error? eof-value] From the Slamhound source: (take-while #(not= ::done %) (repeatedly #(read rdr false ::done))) guns -- 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.