14 Lazy computations, continuations, and the beauty of monadic composition

 

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, 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 Trys. 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.

NOTE

The contents of this chapter are challenging, so don’t be discouraged if you don’t get it all on your first reading.

14.1 The virtue of laziness

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.

14.1.1 Lazy APIs for working with Option

14.1.2 Composing lazy computations

14.2 Exception handling with Try

14.2.1 Representing computations that may fail

14.2.2 Safely extracting information from a JSON object

14.2.3 Composing computations that may fail

14.2.4 Monadic composition: What does it mean?

14.3 Creating a middleware pipeline for DB access