Lazy sequences to replace looping?
Hello, I am just getting started with Clojure and I had a question. I have to following code: (get-rss-entry (get-rss-feeds h-res) url) The call to get-rss-feeds returns a lazy sequence of URLs of feeds that I need to examine. The call to get-rss-entry looks for a particular entry (whose :link field matches the second argument of get-rss-entry). It examines, one-by-one, the lazy sequence returned by get-rss-feeds. Evaluate each item requires an http request across the network to fetch a new rss feed. Therefore, to minimize the number of http requests it's important to examine the sequence one-by-one and stop as soon as there is a match. Here is the code: (defn get-rss-entry [feeds url] (first (drop-while empty? (map #(entry-with-url % url) feeds entry-with-url returns a lazy sequence of matches or an empty sequence if there is no match. First, I want to make sure I understood lazy evaluation correctly and that I did this right. I tested this and it seems to work correctly. Second, not sure if I am solving this problem idiomatically. In Java, for example, this would likely be solved with a loop examining some data structure. At first I did it with a loop/recur but that didn't seem like the right way to do it. Also I saw somewhere that looping in considered low level in Clojure or something to that effect. I would appreciate your feedback. Thanks for your help. -- 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
Re: Lazy sequences to replace looping?
You may find this useful: http://blog.fogus.me/2010/01/22/de-chunkifying-sequences-in-clojure/ loop-recur might be the best way to do it. On Tue, Sep 11, 2012 at 11:16 PM, Geo wrote: > Hello, > > I am just getting started with Clojure and I had a question. I have to > following code: > > (get-rss-entry (get-rss-feeds h-res) url) > > The call to get-rss-feeds returns a lazy sequence of URLs of feeds that I > need to examine. > > The call to get-rss-entry looks for a particular entry (whose :link field > matches the second argument of get-rss-entry). It examines, one-by-one, the > lazy sequence returned by get-rss-feeds. Evaluate each item requires an http > request across the network to fetch a new rss feed. Therefore, to minimize > the number of http requests it's important to examine the sequence > one-by-one and stop as soon as there is a match. > > Here is the code: > > (defn get-rss-entry > [feeds url] > (first (drop-while empty? (map #(entry-with-url % url) feeds > > entry-with-url returns a lazy sequence of matches or an empty sequence if > there is no match. > > First, I want to make sure I understood lazy evaluation correctly and that I > did this right. I tested this and it seems to work correctly. > > Second, not sure if I am solving this problem idiomatically. In Java, for > example, this would likely be solved with a loop examining some data > structure. At first I did it with a loop/recur but that didn't seem like the > right way to do it. Also I saw somewhere that looping in considered low > level in Clojure or something to that effect. I would appreciate your > feedback. > > Thanks for your help. > > -- > 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 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
Re: Lazy sequences to replace looping?
Thanks. I investigated it, which led to another related question as to whether lazy seqs are always chunked (it appears not!). I posted this on stack overflow here: http://stackoverflow.com/questions/12412038/in-clojure-are-lazy-seqs-always-chunked -- 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