Who This Book Is For
I kept several audiences in mind as I wrote this book:
Developers who want a simple introduction to functional programming in Scala
Developers who are interested in writing “better” code
Parallel/concurrent application developers
“Big data” application developers
(Possibly) Upperclass college students
Here’s a quick look at why I say that I wrote this book for these people.
First, because this book started as a series of small notes I made for myself as I learned about functional programming in Scala, it’s safe to say that I wrote it for someone like me who has worked with OOP in Java, but has only a limited FP background. Specifically, this is someone who became interested in Scala because of its clean, modern syntax, and now wants a “simple but thorough” introduction to functional programming in Scala.
Because I’ve also written programs in C, C++, Perl, Python, Ruby, and a few other programming languages, it’s safe to say that this book is written with these programmers in mind as well.
At specific points in this book — such as (a) when writing about pure functions, (b) using val
and not var
, and (c) avoiding the use of null
values — I also wrote this book for any developer that wants to write better code, where I define “better” as safer, easier to test, and more error-free. Even if you decide not to write 100% pure FP code, many FP techniques in this book demonstrate how you can make your functions and methods safer from bugs.
As a personal note, an ongoing theme in my programming life is that I want to be able to write applications faster, without sacrifing quality and maintainability. A selling point of FP is that it enables you to write safe functions — pure functions that rely only on their inputs to produce their outputs — that you can then combine together to create applications.
Quiz: How many cores are in your smartphone? (This question is a tip of the cap to Derek Wyatt, who wrote about CPU cores and smartphones in his book, Akka Concurrency).
In addition to writing safer code, the “killer app” for FP since about 2005 is that CPUs aren’t constantly doubling in speed any more. (See Herb Sutter’s 2005 article, The Free Lunch is Over.) Because of this, CPU designers are adding more cores to CPUs to get more overall CPU cycles/second. Therefore, if you want your apps to run as fast as possible, you need to use concurrent programming techniques to put all of those cores to use, and the best way we know how to do that today is to use FP.
Two of my favorite ways of writing parallel/concurrent applications involve using Scala futures and the Akka messaging/actors framework. Not surprisingly, FP works extremely well with both of these approaches.
Note that if quantum computers were available tomorrow, performance might no longer be an issue, but even in that world we’ll still need to write concurrent applications, and as mentioned, FP is the best way to write parallel and concurrent applications today. I‘ll provide more support for that statement within this book, but one simple thing I can say now is that because there are no mutable variables in FP code, it’s not possible to modify the same variable in different threads simultaneously.
More recently, Dean Wampler gave a presentation titled, “Copious Data: The ‘Killer App’ for Functional Programming”. My experience with Big Data applications is limited to processing large Apache access log records with Spark, but I can confirm that the code I wrote was a lot like algebra, where I passed data into pure functions and then used only the results from those functions. My code had no dependence on “side effects,” such as using mutable variables or managing state.
As I wrote in the Scala Cookbook, because of its “power user” features, I don’t think Scala is a good first language for a programmer to learn, and as a result of that, a book about Scala/FP is also not a good first programming book to read.
That being said, I hope this will be a good first FP book to read after a college student has experience with languages like C, Java, and Scala. When I started writing this book, my nephew was a senior in college and had some experience with C and Java, and as I reviewed the chapters I’d ask myself, “Would Tyler be able to understand this?”
Finally, as a result of the people I have written this book for, it should come as no surprise that this book is not written for FP experts and theorists. I offer no new theory in this book; I just try to explain functional programming using the Scala programming language in the simplest way I can.