Learning Functional Programming in Scala
Alvin Alexander
A logical, step by step approach to learning functional programming
Copyright
Learning Functional Programming in Scala
Copyright 2017 Alvin J. Alexander All rights reserved. No part of this book may be reproduced without prior written permission from the author.
Disclaimer: This book is presented solely for educational purposes, and it’s also a work in progress. While best efforts have been used in preparing this book, the author makes no representations or warranties of any kind and assume no liabilities of any kind with respect to the accuracy or completeness of the contents, and specifically disclaim any implied warranties of merchantability or fitness of use for a particular purpose. The author shall not be held liable or responsible to any person or entity with respect to any loss or incidental or consequential damages caused, or alleged to have been caused, directly or indirectly, by the information or programs contained herein. Any use of this information is at your own risk. The advice and strategies contained herein may not be suitable for your situation.
Version 0.1.1, published May 15, 2017 (The cover photo was taken on the Dalton Highway in Alaska, somewhere between Fairbanks and Prudhoe Bay.)
Contents
1 Changelog 2 Preface
13
3 Introduction
(or, Why I Wrote This Book)
5
11 15 23 33 39 41 55
4 Who This Book is For 5 Goals 6 Question Everything 7 Rules for Programming in this Book 8 One Rule for Reading this Book 9 What is “Functional Programming”? 10 What is This Lambda You Speak Of?
iii
CONTENTS
11 The Benefits of Functional Programming
73
12 Disadvantages of Functional Programming 13 The “Great FP Terminology Barrier” 14 Pure Functions
99
123 131 141 153 163 173 181
15 Grandma’s Cookies (and Pure Functions) 16 Benefits of Pure Functions 17 Pure Functions and I/O 18 Pure Function Signatures Tell All 19 Functional Programming as Algebra 20 A Note About Expression-Oriented Programming 21 Functional Programming is Like Unix Pipelines 22 Functions Are Variables, Too
197 203 225
CONTENTS
iii
23 Using Methods As If They Were Functions
243
24 How to Write Functions That Take Functions as Input
Parameters
255 279 287 303 327 345 347 351 359 367 373
25 How to Write a ‘map’ Function 26 How to Use By-Name Parameters 27 Functions Can Have Multiple Parameter Groups 28 Partially-Applied Functions (and Currying) 29 Recursion: Introduction 30 Recursion: Motivation 31 Recursion: Let’s Look at Lists 32 Recursion: How to Write a ‘sum’ Function 33 Recursion: How Recursive Function Calls Work 34 Visualizing the Recursive sum Function
iv
CONTENTS
35 Recursion: A Conversation Between Two Developers 383 36 Recursion: Thinking Recursively 37 JVM Stacks and Stack Frames 38 A Visual Look at Stacks and Frames 39 Tail-Recursive Algorithms
387 397 407 417 431 437 455 465 477
40 A First Look at “State” 41 A Functional Game (With a Little Bit of State) 42 A Quick Review of Case Classes 43 Update as You Copy, Don’t Mutate 44 A Quick Review of for Expressions 45 How to Write a Class That Can Be Used in a for Expres- sion
487
46 Creating a Sequence Class to be Used in a for Compre-
CONTENTS
v
hension
491
47 Making Sequence Work in a Simple for Loop
499
48 How To Make Sequence Work as a Single Generator in a for Expression
503 509
49 Enabling Filtering in a for Expression 50 How to Enable the Use of Multiple Generators in a for
Expression
519 531 535 543 557
51 A Summary of the for Expression Lessons 52 Pure Functions Tell No Lies 53 Functional Error Handling (Option, Try, or Either) 54 Embrace The Idioms! 55 What to Think When You See That Opening Curly Brace561 A Explaining Scala’s val Function Syntax
581
vi
CONTENTS
- B
- for expression translation examples
599
Chapter 1
Changelog
Version 0.1.1
• released May 15, 2017 • all of the lessons following “The Great FP Terminology Barrier” are new
• the book now contains 54 chapters and two appendices (about 400 pages of new content)
• the book’s formatting is improved (but still needs more work)
Version 0.1.0
• released April 23, 2017 • this was the initial release • it included the first eleven chapters, and was 219 pages long
1
2
Changelog
Chapter 2
Preface
While this Preface is a little unorthodox, I feel like it’s important to state why I’m releasing this book in the way that I’m releasing it.
The first thing to say is that I’ve realized that it’s going to take another year before I can get this book to a point where I’m really going to love it. That being said, even though it’s definitely a “first draft” in its current state, I think there’s some material in here that can help programmers who are struggling to learn functional programming in Scala. I don’t want those people to have to wait another year when I can get something into their hands that can help them today.
Therefore, I’ll be releasing a complete version of this book “as is” as soon as possible. You can think of it as an “alpha” version of what will eventually be the final book.
Release plan
The book will initially be available only as a PDF because I don’t want to commit to print or MOBI (Amazon Kindle) versions of the book until it’s finished.
My goal is to release a first, complete draft of the book by the end of May June, 2017. After that I also hope to provide updates to the book as they become available (i.e., as fast as I can write them).
3
4
Preface
Pricing
I haven’t set a price for the book yet, but since it’s an alpha version I’ll probably just charge a nominal fee, maybe $10-15 (U.S. currency). Hopefully that will be enough to help me keep paying the bills. :)
If it’s technically possible I’d like to make all updates to the alpha version to be free of charge to those who buy the book. My understanding is that I can do this on the platform I plan to use. “Updates” will include bug fixes, better organization, and better graphics, formatting, and improved wording.
At some point I’ll have a “final” version of the book — at which time I’ll commit to print and MOBI, and the price will probably be $20 or more — but I haven’t put much thought into that yet.
About the book
I’m currently wrapping up the final lessons in the book, and I think the final alpha version will contain 90-110 small chapters. After the initial background chapters, each lesson will generally cover a single topic, which is why there will be so many chapters. After several attempts at organizing the material, I think this is the best way to explain everything; it’s easier to understand the concepts when I introduce them one at a time, in small bites.
All the best, Al
Chapter 3
Introduction (or, Why I Wrote This Book)
“So why do I write, torturing myself to put it down? Because in spite of myself I’ve learned some things.”
Ralph Ellison
The short version of “Why I wrote this book” is that I found that trying to learn functional programming in Scala was really hard, and I want to try to improve that situation.
The longer answer goes like this …
My programming background
My degree is in aerospace engineering, so the only programming class I took in college was a FORTRAN class I was forced to take. After college I was one of the youngest people at the aerospace company I worked at, which meant that I’d have to maintain the software applications our group used. As a result, I became interested in programming, and then became interested in (a) “How can I write code faster?”, and then (b) “How can I write maintainable code?”
5
6
Introduction(o r , W hy I Wrote This Book)
After that I taught myself how to program in C by reading the classic book, The C Programming Language by Kernighan and Ritchie, quickly followed by learning Object-Oriented Programming (OOP) with C++ and Java. That was followed by investigating other programming languages, including Perl, PHP, Ruby, Python, and more.
Despite having exposure to all of these languages, I didn’t know anything about Functional Programming (FP) until I came across Google’s Guava project, which includes FP libraries for Java collections. Then, when I learned Scala and came to understand the methods in the Scala collections’ classes, I saw that immutable values and pure functions had some really nice benefits, so I set out to learn more about this thing called Func-
tional Programming.
Trying to learn FP with Scala
As I tried to learn about FP in Scala, I found that there weren’t any FP books or blogs that I liked — certainly nothing that catered to my “I’ve never heard of FP until recently” background. Everything I read was either (a) dry and theoretical, or (b) quickly jumped into topics I couldn’t understand. It seemed like people enjoyed writing words “monad” and “functor” and then watching me break out in a cold sweat.
As I googled “scala fp” like a madman, I found a few useful blog posts here and there about functional programming in Scala — what I’ll call “Scala/FP” in this book — but those were too disconnected. One article covered Topic A, another covered Topic Z, and they were written by different authors with different experiences, so it was hard to find my way from A to Z. Besides being disjointed, they were often incomplete, or maybe they just assumed that I had some piece of knowledge that I didn’t really have.
7
Another stumbling block is that experienced FP developers use generic types a lot. They also use the word “easy” when describing their code, as though saying “easy” is some sort of Jedi mind trick. For instance, this code — which I’ll break down as you go through this book — was introduced with the text, “it’s very easy to access and modify state”:
def updateHealth(delta: Int): Game[Int] =
StateT[IO, GameState, Int] { (s: GameState) =>
val newHealth = s.player.health + delta IO((s.copy(player = s.player.copy(health = newHealth)), newHealth))
}
I don’t know about you, but the first time I saw that code, the word easy is not what came to mind. What came to my mind were things like, “PHP is easy. Using setter methods to modify state is easy. Whatever that is … that’s not easy.”
Another problem with almost all of the Scala/FP resources is that they don’t discuss functional input/output (I/O), or how to work with user interfaces. In this book I don’t shy away from those topics: I write what I know about both of them.
Learning Haskell to learn FP
In the end, the only way I could learn FP was to buy four Haskell books, take a few weeks off from my regular work, and teach myself Haskell. Because Haskell is a “pure” FP language — and because most experienced Scala/FP developers spoke glowingly about Haskell — I assumed that by learning Haskell I could learn FP. 8
Introduction(o r , W hy I Wrote This Book)
That turned out to be true. In Haskell the only way you can write code is by using FP concepts, so you can’t bail out and take shortcuts when things get difficult. Because everything in Haskell is immutable, I was forced to learn about topics like recursion that I had avoided for most of my programming life. In the beginning this made things more difficult, but in the end I learned about the benefits of the new approaches I was forced to learn.
Once I understood Haskell, I went back to the Scala resources that I didn’t like before and they suddenly made sense(!). But again, this only
happened after I took the time to learn Haskell, a language I didn’t plan on
using in my work.
The purpose of this book
Therefore, my reasons for writing this book are:
• To save you the time of having to try to understand many different, unorganized, inconsistent Scala/FP blog posts
• To save you the time of “having to learn Haskell to learn FP,” and then having to translate that Haskell knowledge back to Scala
• To try to make learning Scala/FP as simple as possible
9
Don’t get my statements about Haskell wrong: In the end, Haskell turned out to be a really interesting and even fun programming language. If I knew more about its libraries — or if it ran on the JVM and I could use the wealth of existing JVM libraries out there (most of which are not written in an FP style) — I’d be interested in trying to use it. That being said, I hope I can teach you what I learned about FP using only Scala.
As a potential benefit of this book, if you already know Scala/OOP and are interested in learning Haskell, you can learn Scala/FP from this book, and then you’ll find it much easier to understand Haskell.
10
Introduction(o r , W hy I Wrote This Book)