Working with lazy sequences

There's another subtle notion to grasp when we talk about infinite lazy sequences. If we normally assign (or bind, in this case) some list or collection, we expect those artifacts to have values right away. You may have noticed that even after binding pricelist and timeseries, nothing happened. This is to say that binding these two Vars didn't immediately compute any values from the aforementioned expressions. This is the main feature that laziness provides. Only when we needed to pull out values using, for example, (take 40 (generate-prices 5 15)) or (take 2 our-average), were the expressions actually run. This means that (def timeseries (generate-timeseries pricelist) bound a lazy sequence to the timeseries symbol. We can also pass round this lazy sequence without necessarily consuming it.

In Down The Clojure Rabbit Hole, Clojure developer, Christophe Grand, describes sequences as "Reified Computation" and as a core part of Clojure's computation model, which can be seen at http://www.infoq.com/presentations/clojure-stories. By reified, what he's referring to is to literally realize computation. This allows us to treat sequences as a data flow pipeline. This just means that we can compose functions, and pieces of functions with greater ease as long as everybody's using lazy sequences.

The idea here is that when data enters your system from the outside world, it is mostly a more concrete form (such as lists, vectors, and so on). However, most core Clojure functions optimally consume and produce sequences. You may have also noticed that most of the core functions we've used so far (repeatedly, filter, map, and so on) also produce sequences. If we can begin to visualize data flowing through our systems, it becomes very useful to swap different functions that can perform anything from analysis to transformation and so on. It allows what software developers describe as loose coupling (http://en.wikipedia.org/wiki/Loose_coupling) between functions and components. We can thus allow data and its desired outputs to drive design.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.126.199