laziness programming

Laziness with Clojure

It took me a long time to fully grasp the concept of laziness and eagerness despite working with Clojure for the past few months. As a result, I decided to try and concisely explain the concepts here for any other beginners and also in an attempt to consolidate my understanding. A huge thanks to the book The Joy of Clojure* by Michael Fogus and Chris House. It provided the clearest explanation thus far (for me) on the topic and will be the basis for this post along with a few other sources.

First, what is laziness? Briefly put, it is an evaluation strategy where expressions are evaluated only when their values are needed. I know, that makes little sense at the moment, but bear with me. Second, why should we care? To quote Fogus and House, “the fundamental reason for laziness in Clojure [is] the avoidance of full realization of interim results.” Take that line in, recognize the powerful implications it has. We’ll discuss more after the break.

(more…)