Book Image

Learn Scala Programming

By : Slava Schmidt
Book Image

Learn Scala Programming

By: Slava Schmidt

Overview of this book

The second version of Scala has undergone multiple changes to support features and library implementations. Scala 2.13, with its main focus on modularizing the standard library and simplifying collections, brings with it a host of updates. Learn Scala Programming addresses both technical and architectural changes to the redesigned standard library and collections, along with covering in-depth type systems and first-level support for functions. You will discover how to leverage implicits as a primary mechanism for building type classes and look at different ways to test Scala code. You will also learn about abstract building blocks used in functional programming, giving you sufficient understanding to pick and use any existing functional programming library out there. In the concluding chapters, you will explore reactive programming by covering the Akka framework and reactive streams. By the end of this book, you will have built microservices and learned to implement them with the Scala and Lagom framework.
Table of Contents (19 chapters)

Free monads

In this chapter and the previous chapters, we represented sequenced computations with monads. The flatMap method of the monad describes how the computation steps should be joined and the function given to it as an argument—the computation step itself. The free monad elevates the concept of sequenced computations to the next level.

First, we start to represent the computation steps as instances of some ADT (algebraic data type) of our choice. Second, we represent the monadic concept with instances of another ADT.

To substantiate this approach, we can turn to the fishing example once again. Earlier, we had three actions we encoded as functions. These actions will be represented as value classes now. We also need to give specific meaning to the type aliases we've used before to be able to run examples later.

Here is the definition of the fishing model and...