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)

What this book covers

Chapter 1, An Introduction to Scala 2.13, walks the reader through Scala 2.13. Apart from helping readers who are new to Scala, the book will help developers who are already familiar with Scala to start using the new version and give them the motivation to migrate.

Chapter 2, Understanding Types in Scala, transforms the reader into a type master. It starts by describing different ways to define types in Scala, as well as refreshing the reader's memory about type bindings, variance, and bounds. This foundation will establish a stable groundwork for explaining how types can be combined to form patterns and the situations in which these patterns are applicable.

Chapter 3, Deep Dive into Functions, starts out by going over the different ways of defining a function in Scala. Then, it will cover intermediate aspects, such as higher-order functions, polymorphism, and recursion. Finally, it will discuss advanced aspects, such as trampolining and the object-oriented nature of functions in Scala.

Chapter 4, Getting to Know Implicits and Type Classes, teaches parts of the language related to implicits, in particular, different types of implicits, the order of implicit scope resolution, and context bounds. Moving on, the chapter will explain machinery that facilitates the use of modern functional programming in Scala as we know it – type classes.

Chapter 5, Property-Based Testing in Scala, touches upon different ways to style Scala test code and use its facilities to test asynchronous and concurrent functionality. The reader will be taken through the concept of property-based testing and its practical applications in depth.

Chapter 6, Exploring Built-in Effects, covers built-in Scala effects, such as Option, Either, Try, and Future. It describes the scenarios in which it is appropriate to use one or, other abstraction and reveals the proper ways to do so. Most importantly, it helps the reader discover similarities between these seemingly different abstractions.

Chapter 7, Understanding Algebraic Structures, begins by identifying common patterns in combining data. It teaches you how these patterns are named and demonstrate how they can be implemented in Scala using type classes.

Chapter 8, Dealing with Effects, deals with effects the same way we dealt with algebraic structures in the previous chapter. First, the chapter provides motivation in the form of real use cases for effects, and describes why they are useful and even crucial. Then, it extends this intuition by providing a formal description. Finally, it walks the reader through the example implementation.

Chapter 9, Familiarizing Yourself with Basic Monads, continues consolidating abstract concepts by combining algebraic data types and effects defined in the two previous chapters into yet another powerful concept – monads. It explains why monads are necessary, describes monadic laws, and shows example implementations for a small number of essential monads and monad transformers.

Chapter 10, A Look at Monad Transformers and Free Monad, outlines the problems introduced by monad transformers. It explores a free monad and the possibilities they provide. Finally, it gives due attention to the new kid on the block – an IO monad.

Chapter 11, An Introduction to the Akka and Actor Models, outlines the need for and principles of event-based programming. It then demonstrates how to implement a simple actor-based application using classical Akka. Finally, we drop the most important Akka modules into the mix and help to understand various testing techniques.

Chapter 12, Building Reactive Applications with Akka Typed, reveals an alternative way to build reactive applications with Akka. It introduces Akka Typed, compares classical and typed approaches, and shows the reader how the latter reduces a developer's choices but increases type safety and simplifies reasoning concerning actor-based programs.

Chapter 13, Basics of Akka Streams, describes yet another way to structure reactive programs – reactive streams. It catalogs the basic building blocks of typical Akka Stream applications and discusses techniques for wiring them together. Toward the end, it covers the topics of error handling and testing.

Chapter 14, Project 1 - Building Microservices with Scala, covers two topics. First, it substantiates the need for microservices and describes their advantages and building principles. It also uncovers technical and organizational challenges related to the microservice-based approach. Second, it uses the knowledge the reader has gained from previous chapters to build two actual projects from scratch. Both projects represent simple microservices implementing, CRUD API with access to a database, validation, and data transformation. The first project will be based on knowledge obtained from first and second sections of the book. We will build a microservice using the open source functional programming libraries, http4s, and circe, for client API, monocle to demonstrate the use of optics, and doobie for database access. The second project will be built using reactive programming libraries and techniques covered in the third section of the book, Akka, and Akka HTTP. For database access, well use Lightbend Slick, while marshalling and unmarshalling will be done using Spray-JSON.

Chapter 15, Project 2 - Building Microservices with Lagom, goes into the details of the Lagom framework by discussing its philosophy, the way Lagom applications are structured, available APIs and how to use them, and the topics of running and deployment. This knowledge will be used by the reader to build a project where they will create an application from scratch and deploy an example Lagom application.

Appendix A, Preparing the Environment and Running Code Samples, provides detailed installation instructions for Java and SBT for all major platforms. It also contains useful suggestions about checking out accompanying code from the Github repository as well as working with SBT shell and Scala REPL.