On Aug 31, 4:44 pm, wangzx <[email protected]> wrote:
> Is there other APIs like the Sequence but provide stream-like API?
You can try this, which I've used on short files recently:
(defn #^Class class-identity [#^Class c] c)
(defn #^java.io.BufferedReader reader-from-classpath [#^String s]
(-> (. (class-identity java.lang.String) getResourceAsStream s)
(java.io.InputStreamReader.)
(java.io.BufferedReader.)))
;;perhaps this is the function you are looking for?
(defn buf2seq [#^java.io.BufferedReader b]
(lazy-seq
(if-let [l (.readLine b)]
(cons l (buf2seq b)))))
(defmacro with-open-stream [bindings body]
(let [varname (bindings 0)
resource (bindings 1)]
`(let [s# (reader-from-classpath ~resource)
~varname (buf2seq s#)]
(try ~body
(finally (if-not (nil? s#)
(try (.close s#))))))))
(def prop-map
(with-open-stream [s "/resources/myresource.properties"]
(into {} (map #(into [] (.split #^String % "=")) s))))
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---