Introduction to Functional Programming with Scala

This book is my translation and adaptation of a series of English-language articles on functional programming in Scala. I've added many new details, examples, and exercises based on my own experience as a practitioner of functional programming.

What this book is

This book is a practical guide to functional programming in Scala. It's designed for developers who want to use the power of functional programming to write reliable and elegant code. The book assumes basic familiarity with Scala and the general principles of programming.

What this book is not

This is not an introductory programming textbook. It's also not a deep dive into the mathematical foundations of functional programming. Instead, it's a practical guide for developers who want to use functional programming in their daily work.

What you will learn from this book

After reading this book, you will be able to:

Book structure

Part 1. Functional paradigms

This part introduces the basic concepts and paradigms of functional programming. In the first chapters you will learn the main features of functional programming: immutability, pure functions, and the main advantages and disadvantages of this paradigm.

You will also learn about the key principles of functional programming and functional patterns: immutability, higher-order functions, referential transparency, currying, composition of functions, lazy evaluation, and pattern matching. The first part will also introduce the Reader monad as the main tool for working with a global environment in functional style.

Part 2. Data representation

This part is dedicated to the use of algebraic data types (ADT) for modeling domain models. You will learn about sum and product types, the types of Option, Either, and Validated, their purpose, and how to use them when modeling applications. You will also learn about cats and Discipline.

Part 3. Effect systems

In the third part you will learn about the basics of functional programming and how to work with effect systems in Scala. We will discuss the monad, the use of the Try, Future, and Either types in the standard library. In this part you will learn about State monad, Cokleisli, Bimonad, and Free monad. You will also learn how to build your own effect system using Free monad. Finally, you'll learn how to build a custom effect system using the Free monad.

Next, you will learn the basics of the Cats Effect library and how to use it to work with the IO effect. You will learn about effect construction and execution, effect memoization, and effect composition using flatMap, map, and product. The Cats Effect part also includes the Concurrent typeclass for working with concurrent effects using Concurrent[IO], the Resource type for resource management, Async typeclass for working with external asynchronous APIs using callbacks or Future, and Scheduler for managing delays and periodic tasks.

Then you will learn about ZIO: the ZIO effect type, ZEnvironment, ZLayer for dependency injection, ZStream for composing effects, ZIO and ZQueue for working with concurrent effects, Deferred, Ref, Promise, CountDownLatch, and TMap, ZPool for thread pool management, and TArray and transactional memory.

Finally, you will learn the basics of Reader monad and Environment, the main tool for working with a global environment in functional style.

Part 4. Advanced features

The final part of the book covers advanced language features and techniques used in real projects: working with typeclasses, the macro system, and type-level programming.

Additional chapters

In this edition of the book, I've added several chapters based on my personal experience and practical problems that I've encountered in my work. These chapters explore topics that I found interesting or important. You'll find sections on SICP (which will be periodically updated) at the end of the book.

Code examples

All examples in this book are written in Scala 3. To run them, you will need at least Scala version 3.7.1.

To run code examples, use the dotty interpreter, located in the examples/modules/scalabook-examples/src/main/scala-3 directory.

Conventions

Exercises

Each chapter contains exercises to consolidate the material. They range from simple questions to tasks that require writing small programs.

Feedback

I would appreciate your feedback and suggestions. If you find a mistake or have ideas for improving the material, please let me know through GitHub issues.