As Mauricio already mentioned, map is not meant to be used for side-effects, if you want to need to evaluate a sequence for side-effects, use doseq <http://clojuredocs.org/clojure_core/clojure.core/doseq> instead. Regarding your first question about explicit loop-recur, i think it's reasonable way to write the functionality you want to achieve, even if the for macro is maybe little more idiomatic and shorter/more concise, here is the comparison:
(def url-sequence (map #(str "http://www.mysite.com/list.php?pageID=" %) (range))) (def download (loop [urls url-sequence lst []] (let [u (first urls) r (rest urls) resp (client/get u) next (re-find #"(s?)title=\"next page\">Next >>" (:body resp)) segments ((html2data (:body resp)) :segment) data (map segment2data segments)] (if next (recur r (conj lst data)) (conj lst data))))) (def download (->> (for [url url-sequence :let [body (:body (client/get url)) next-pages (re-find #"(s?)title=\"next page\">Next >>" body)] :while next-pages] (map segment2data (-> body (html2data) :segment)))) (into [])) Also note that i didn't use local binding named next, but instead i named it next-pages, because next local name could lead to surprise if you forget that you aliased something to next and try to use clojure core next function :) Dňa pondelok, 13. januára 2014 15:49:50 UTC+1 Kashyap CK napísal(-a): > > Hi, > I've been dabbling with Clojure for a bit and I am really loving it!!! > > I am trying to write a clojure program to download a bunch of URLs and I > was wondering what is the right way to do it - > My current implementation is as follows > > (def url-sequence (map #(str "http://www.mysite.com/list.php?pageID=" %) > (range))) > > (def download > (loop [urls url-sequence lst []] > (let > [ > u (first urls) > r (rest urls) > resp (client/get u) > next (re-find #"(s?)title=\"next page\">Next >>" (:body > resp)) > segments ((html2data (:body resp)) :segment) > data (map segment2data segments) > ] > (if next > (recur r (conj lst data)) > (conj lst data))))) > > I was wondering if I could replace the loop thingy with a map - that is > when I experimented with a simple piece of code and found something that > was not intuitive. > I'd really appreciate it if someone could tell me what's going on when I > do the following - > > user> (defn xx [] (map println [1 2 3 4 5 6])) > #'user/xx > user> (take 2 (xx)) > (1 > 2 > 3 > 4 > 5 > 6 > nil nil) > user> > > Regards, > Kashyap > -- -- 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/groups/opt_out.