We begin this book with a radical and restrictive premise: we will be constructing programs using only pure functions with no side effects, such as reading from files or mutating memory. This idea, of FP, leads to a very different way of writing programs than you may be used to. We therefore start from the very beginning, relearning how to write the simplest of programs in a functional way.
In the first chapter, we’ll explain exactly what FP means and give you some idea of its benefits. The rest of the chapters in part 1 introduce the basic techniques for functional programming in Scala. Chapter 2 introduces the Scala language and covers fundamentals, including writing loops functionally and manipulating them as ordinary values. Chapter 3 deals with in-memory data structures that may change over time, and chapter 4 talks about handling errors in pure functions. Chapter 5 then introduces the notion of non-strictness, which can be used to improve the efficiency and modularity of functional code. Finally, chapter 6 introduces modeling stateful programs using pure functions. The intent of this first part of the book is to get you thinking about programs purely in terms of functions from inputs to outputs and teach you the techniques you’ll need in part 2 when we start writing some practical code.