Being Lazy
Web Development with Clojure, Third Edition — by Dmitri Sotnikov, Scot Brown (73 / 107)
👈 Threading Expressions | TOC | Structuring th e Code 👉
Many Clojure algorithms use lazy evaluation, where the operations aren’t performed unless their result actually needs to be evaluated. Laziness is crucial for making many algorithms work efficiently. For example, you might think the preceding example is very inefficient since we have to iterate over the sequence each time to create the range, map across it, interpose the numbers, and reduce the result.
However, this isn’t actually the case. The evaluation of each expression happens on demand. The first value in the range is generated and passed to the function, then the next, and so on, until the sequence is exhausted. This is similar to the approach that languages like Python take with their iterator mechanics.
👈 Threading Expressions | TOC | Structuring th e Code 👉
Web Development with Clojure, Third Edition by Dmitri Sotnikov, Scot Brown can be purchased in other book formats directly from the Pragmatic Programmers. If you notice a code error or formatting mistake, please let us know here so that we can fix it.