This chapter covers
- Lazy computations
- Exception handling with
Try
- Monadically composing functions
- Escaping the pyramid of doom with continuations
In this chapter, you’ll first learn why it’s sometimes desirable to define lazy computations; that is, functions that may or may not be evaluated. You’ll then see how these functions can be composed with other functions, independently of their evaluation.
Once you’ve got your feet wet with lazy computations, which are just plain functions, you’ll see how the same techniques can be extended to computations that have some useful effect other than laziness. Namely, you’ll learn how to use the Try
delegate to safely run code that may throw an exception, and how to compose several Try
s. You’ll then learn how to compose functions that take a callback without ending up in “callback hell.”
What holds all these techniques together is that, in all cases, you’re treating functions as things that have certain specific characteristics, and you can compose them independently of their execution. This requires a leap in abstraction, but the result is quite powerful.
The contents of this chapter are challenging, so don’t be discouraged if you don’t get it all on your first reading.
Laziness in computing means deferring a computation until its result is needed. This is beneficial when the computation is expensive and its result may not be needed.