preface

Hello! Thanks for purchasing Grokking Functional Programming . I’ve spent the last decade talking with programmers about the approach to programming, its maintainability, and how functional programming concepts are slowly being adopted by mainstream languages. Many of those professional developers say it’s still very difficult to learn functional concepts from existing sources, which are either too simplistic or too complex. That’s the gap this book is trying to fill. It aims to provide a step-by-step practical guide for programmers who want to get the full picture of fundamental functional programming concepts.

People learn best from examples, and that’s why this book is heavy with them. Theory always comes second. After finishing this introductory book, you’ll be able to write fully featured programs using functional programming and comfortably dive into its theoretical foundations.

You’ll get the most benefit from this book if you’ve already created some non-trivial applications using an imperative object-oriented language like Java or Ruby. It’s a big plus if you’ve worked on a team who struggled with lots of bugs and maintainability issues, because this is where functional programming shines the most.

I hope you’ll enjoy reading the chapters and solving the exercises as much as I enjoyed writing them. Thanks again for your interest in the book!

—Michał Płachta

acknowledgments

I’d like to first of all thank the Scala community for its continuous pursuit of tools and techniques that help build maintainable software. All of the ideas presented in the book are the result of countless hours of code reviews, discussions, multiple back-and-forth blog articles, hot-take presentations, and production outage postmortems. Thank you all for your passion.

I’d like to thank my family, especially my wife Marta, for supporting me during the writing of this book with huge amounts of encouragement and love. Many thanks go to my wonderful kids, Wojtek and Ola, for making sure I don’t sit at the computer for too long.

This book has been the work of many people. I’d like to thank the staff at Manning: Michael Stephens, acquisitions editor; Bert Bates, editor; Jenny Stout, development editor; Josh White, technical development editor; Christian Berk, copy editor; Keri Hales, production editor; Ubaldo Pescatore, technical proofreader; Katie Tennant, proofreader; and all of the behind-the-scenes folks who helped get this book into print.

To all the reviewers: Ahmad Nazir Raja, Andrew Collier, Anjan Bacchu, Charles Daniels, Chris Kottmyer, Flavio Diez, Geoffrey Bonser, Gianluigi Spagnuolo, Gustavo Filipe Ramos Gomes, James Nyika, James Watson, Janeen Johnson, Jeff Lim, Jocelyn Lecomte, John Griffin, Josh Cohen, Kerry Koitzsch, Marc Clifton, Mike Ted, Nikolaos Vogiatzis, Paul Brown, Ryan B. Harvey, Sander Rossel, Scott King, Srihari Sridharan, Taylor Dolezal, Tyler Kowallis, and William Wheeler, thank you; your suggestions helped make this a better book.

about this book

Who should read this book

The book assumes that the reader has at least one year of commercial experience developing software using a mainstream object-oriented programming language like Java. Examples use Scala as the teaching language, but this is not a Scala book. No prior knowledge of Scala or functional programming is required.

How this book is organized: A road map

The book is divided into three parts. The first part lays the foundation. We will learn tools and techniques that are ubiquitous in functional programming (FP). In chapter 1, we will discuss how to learn FP with this book. In chapter 2, we will show the difference between pure and impure functions. In chapter 3, we will introduce the immutable value. Finally, in chapter 4, we will show how pure functions are just immutable values and demonstrate all the superpowers that we get from this fact.

In the second part of the book, we will use only immutable values and pure functions to solve real-world problems. In chapter 5, we will introduce the most important function in FP and show how it helps in building sequential values (and programs) in a concise and readable way. In chapter 6, we will learn how to build sequential programs that may return errors. We will use chapter 7 to learn about functional software design. Chapter 8 will teach you how to deal with an impure, external, side-effectful world in a safe and functional way. Then, we will introduce streams and streaming systems in chapter 9. We will build streams of hundreds of thousands of items using the functional approach. In chapter 10, we will finally create some functional and safe concurrent programs.

In the third part we will implement a real-world functional application that uses Wikidata as a data source. We will use it to highlight everything we learned in the previous parts. In chapter 11, we will need to create an immutable-based data model and use proper types, including IO , to integrate with Wikidata, use caching and multiple threads to make the application fast. We will wrap all of these concerns in pure functions and additionally show how we can reuse our object-oriented design intuitions in the functional world. In chapter 12, we will show how to test the application we developed in chapter 11 and how easy it is to maintain, even in the presence of big requirement changes.

Finally, we will wrap up the book with a final set of exercises that will make sure you grokked functional programming.

About the code

This book contains many examples of source code both in numbered listings and in line with normal text. In both cases, source code is formatted in a fixed-width font like this to separate it from ordinary text. Sometimes code is also in bold to highlight code that has changed from previous steps in the chapter, such as when a new feature adds to an existing line of code.

In many cases, the original source code has been reformatted; we’ve added line breaks and reworked indentation to accommodate the available page space in the book. Code annotations accompany many of the listings, highlighting important concepts.

You can get executable snippets of code from the liveBook (online) version of this book at https://livebook.manning.com/book/grokking-functional-programming . The complete code for the examples in the book is available for download from the Manning website at https://www.manning.com/books/grokking-functional-programming , and from GitHub at https://github.com/miciek/grokkingfp-examples . All book resources, including bonus materials, are available at https://michalplachta.com/book/ .

liveBook discussion forum

Purchase of Grokking Functional Programming includes free access to liveBook, Manning’s online reading platform. Using liveBook’s exclusive discussion features, you can attach comments to the book globally or to specific sections or paragraphs. It’s a snap to make notes for yourself, ask and answer technical questions, and receive help from the author and other users. To access the forum, go to https://livebook.manning.com/book/grokking-functional-programming/discussion . You can also learn more about Manning’s forums and the rules of conduct at https://livebook.manning.com/discussion .

Manning’s commitment to our readers is to provide a venue where a meaningful dialogue between individual readers and between readers and the author can take place. It is not a commitment to any specific amount of participation on the part of the author, whose contribution to the forum remains voluntary (and unpaid). We suggest you try asking the author some challenging questions lest his interest stray! The forum and the archives of previous discussions will be accessible from the publisher’s website as long as the book is in print.

about the author

Michał Płachta is an experienced software engineer and an active contributor to the functional programming community. He regularly speaks at conferences, runs workshops, organizes meetups, and blogs about creating maintainable software.