

Clojure.core/lazy-seq makes sense - neotyk
http://codemeself.blogspot.com/2010/06/clojurecorelazy-seq-makes-sense.html

======
grogers
Some times in clojure I've found it easier to express a sequence recursively,
rather than iteratively. Using lazy-seq is not only braindead simple for this
- just wrap the whole function in it, it is the only way to keep your stack
use from growing on the recursive calls. I haven't compared the performance of
this to eager evaluation but I'd guess it isn't that poor.

------
morphir
when should you use lazy opposed to traditional eager evaluation? (I
understand how it works) But I don't see exactly when lazy would be the
preferred evaluation. It smells a bit like premature optimization.

~~~
neotyk
Thanks for feedback. If your seq is created by some expensive computation
(i.e. primes) than it makes a lot of sense to do it lazy.

~~~
morphir
so by expensive you mean a growth of n^2 or n^3, worse?

~~~
neotyk
It depends on your application. If you don't know how many elements you'll
need and creation is constant time, but lengthy, than go lazy. On the other
hand if you only need couple of elements that are O(n^3) or worse, it might be
suitable to do non-lazy. It really depends on you application requirements.
Good thing is that if you implement it in first place non-lazy and later
decide that you need it lazy, "interface" will not change.

One thing that you can not do for sure with non-lazy is to construct infinite
seq. Even when you create one lazy way, you have to "loose head" so elements
can get GCed.

HTH

