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)


The monoid identity property allows us to handle empty collections in a general way. So, instead of having the following:

  def reduceLeft(op: (A, A) => A): A

We'll have a definition that takes an identity element as another parameter. By convention, this approach is called fold:

  def foldLeft(identity: A)(op: (A, A) => A): A

The reason for the name foldLeft is that the identity element is used as an initial argument for reducing the collection, which leads to the following sequence of calls:

op(op(op(op(identity, a1), a2), a3), a4), ...

Optionally, it is represented in postfix-notation:

(((identity op a1) op a2) op a3) ...

Which is, well, kind of folding the collection, starting with the identity and the first element of it.

The associativity of the operation and the identity element tells us that another approach is also possible, starting from the identity...