If you want caching behavior, then use it as a sequence. If you want faster 
iteration, use it via reduce. The whole point of this change is to open the 
faster reduce path, which you won't get if you also cache.

I did some research a few months back via crossclj looking at how people 
typically use iterate. I can't actually remember finding any use cases other 
than inc or something similar (but it's been a while).



> On Apr 2, 2015, at 6:11 PM, Nicola Mometto <brobro...@gmail.com> wrote:
> 
> 
> The recent changes to iterate come with an interesting consequence:
> reducing an iterate multiple times will cause the entire chain of x, (f
> x), (f (f x)) .. to be recalculated every time.
> 
> I'd argue that this is not desiderable and a regression (even though
> probably one considered by design), and that this change in behaviour is
> going to produce a degradation in performance when the function to
> iterate actually does some computation rather than being a trivial
> function like the one used in the benchmarks in the CLJ-1603 ticket
> (inc) since its result won't be cached.
> 
> If changing the new behaviour is out of the question I'd suggest to at
> least document this new behaviour. The docstring of iterate talks about
> returning a lazy-seq while when used with reduce it is actually a
> generator.
> 
> To show what I mean here's a silly example using Threa/sleep to simulate
> computation:
> 
> Clojure 1.7.0-alpha5
> user=> (def a (iterate #(do (Thread/sleep 2) (inc %)) 0))
> #'user/a
> user=> (time (reduce (fn [_ x] (if (= 50 x) (reduced nil))) nil a))
> "Elapsed time: 106.385891 msecs"
> nil
> user=> (time (reduce (fn [_ x] (if (= 50 x) (reduced nil))) nil a))
> "Elapsed time: 0.560275 msecs"
> nil
> 
> Clojure 1.7.0-master-SNAPSHOT
> user=> (def a (iterate #(do (Thread/sleep 2) (inc %)) 0))
> #'user/a
> user=> (time (reduce (fn [_ x] (if (= 50 x) (reduced nil))) nil a))
> "Elapsed time: 109.088477 msecs"
> nil
> user=> (time (reduce (fn [_ x] (if (= 50 x) (reduced nil))) nil a))
> "Elapsed time: 109.51494 msecs"
> nil
> 

-- 
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.

Reply via email to