Hi,
Good inital effort. Only a couple of things to comment on:
- You're not closing the stream after you're done with it
This is a very common bug and for that reason clojure provides a macro
- with-open - that takes care of closing the resource for you.
- As you're using a string as the accumulator, you're creating a new
string object at each iteration:
A better approach might be using a StringBuffer to accumulate the
result without wasteful object allocation.
In fact, both of these points are addressed by a core clojure function
called 'slurp'. Here's its source and sample usage:
(defn slurp
([f & opts]
(let [opts (normalize-slurp-opts opts)
sb (StringBuilder.)]
(with-open [#^java.io.Reader r (apply jio/reader f opts)]
(loop [c (.read r)]
(if (neg? c)
(str sb)
(do
(.append sb (char c))
(recur (.read r)))))))))
(println (slurp "http://google.com"))
You can read more about with-open in this link:
http://clojuredocs.org/clojure_core/clojure.core/with-open
Cheers,
Leonardo Borges
www.leonardoborges.com
On Mon, Nov 18, 2013 at 2:00 PM, <[email protected]> wrote:
> Hi everyone,
>
> I'm new to Clojure, and after a lot of reading I wrote a couple of
> functions.
> They are working and doing what they are supposed to, but I was wondering if
> the way I wrote the functions was optimal and if I made any conceptual
> errors which advanced programmers avoid.
> Basically: Are they what they call the "clojure" way?
>
> (defn download-source [url]
> (let [stream (java.io.BufferedReader. (java.io.InputStreamReader. (..
> (java.net.URL. url) openStream)))]
> (loop [b (.readLine stream), acc ""]
> (if (= b nil) acc
> (recur (.readLine stream) (str acc b))))))
>
> (println (download-source "http://google.com"))
>
> This function for example downloads the source of a webpage and returns it
> as a string.
> Could this have been written in a better way?
> I want to get a feeling of what is considered good practice/design in
> clojure.
>
> Nice Regards
>
> --
> --
> 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
> ---
> 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 [email protected].
> For more options, visit https://groups.google.com/groups/opt_out.
--
--
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
---
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 [email protected].
For more options, visit https://groups.google.com/groups/opt_out.