Summary

Monads are arguably the most ubiquitous abstraction in functional programming. Unfortunately they cannot be composed in general—in contrast to functions and applicatives.

Monad transformers provide a way to work around this limitation by specifying a set of overarching structures to represent combinations of monads, each combination being specific to a single internal effect type. Monad transformers compose monads in a way that it is possible to cross both effects with a single call of the flatMap or map.

Monad transformer stacks lift the concept of monad transformers one level higher, utilizing the fact that each monad transformer is also a monad. By stacking monad transformers, it is possible to work with virtually any number of effects combined together in a single pile the same way we would do with a single monad.

Monad transformers are not without disadvantages. The list includes increased garbage collection footprint and processor utilization because of the need to unpack and repack  effects in the stack. The same reasoning applies to the mental model developers needed to build and maintain in their head while working with the code.

The free monad provides a reasonable alternative by clearly separating structure and interpretation of the computations. It does so by representing business logic as a sequence of steps encoded as data by some ADT and executing these steps with suitable interpreter(s).

This chapter concludes the second part of the book. In this part and the first part, we refrained from using third-party libraries and focused on giving readers a deep understanding of the language features and underlying theoretical concepts.

Needless to say, the code examples in this part were decidedly simplistic and only suitable for learning purposes. 

Specifically to the functional programming aspects, there are two exceptionally good libraries, worth mentioning one more time and available for Scala: Cats (https://typelevel.org/cats/) and Scalaz (https://github.com/scalaz/scalaz). If we managed to ignite your interest in programming Scala using the functional style shown in this part of the book, we highly recommend taking a look at both of them. Besides offering production-ready implementation for the concepts we studied, they also contain lots of abstractions we weren't able to discuss.

In the third part of the book we will relax our self-imposed constraint about third-party dependencies and dedicate it to the topic of reactive programming in Scala using different Akka libraries.

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

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