Learn Functional Programming Without Fear
A former Java/OOP teacher
takes you to the cusp of using
Scalas FP libraries, in a small book.
Alvin Alexander
Copyright
Learn Functional Programming Without Fear
Copyright 2022 Alvin J. Alexander
All rights reserved. No part of this book may be reproduced without prior written permission from the author.
This book is presented solely for educational purposes. While best efforts have been made to prepare this book, the author makes no representations or warranties of any kind and assumes no liabilities of any kind with respect to the accuracy or completeness of the contents, and specifically disclaims 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.
Version 1.0, published November 5, 2022
Buy the book and find/report issues: alvinalexander.com/scala/learn-functional-programming-book
Other books
Other books by Alvin Alexander:
- The Scala Cookbook (700+ pages)
- The best-selling and highly-rated book,
Functional Programming, Simplified (also 700+ pages) - My latest book, Learn Scala 3 The Fast Way!
Gratitude
I want to express my thanks to everyone who has made Scala such a wonderful programming language. It has been a fun language to write about.
I also want to express my thanks for Sriram Natarajan, who volunteered three pages of terrific comments on an early release of this book.
I also want to thank my wife, because at the very least, without her support, this book would absolutely not be possible.
Finally, I should thank all the friends and family that keep me grounded. If it wasnt for you, I might still be meditating in the mountains of Alaska. :)
1. The Goal of This Book
This book has an audacious goal: To demonstrate as simply as possible the benefits of:
- Immutable variables (algebraic variables)
- Immutable data structures
- Pure functions
- Expression-oriented programming
- Functional error handling
This books premise is that once youre comfortable with these concepts, youll find that youre writing functional programming (FP) code. At the end of the book I demonstrate this by showing several examples using ZIO, which is one of Scalas two main FP libraries (along with Cats Effect).
Put more clearly: This book gets you ready to start using ZIO and Cats Effect. When you finish this book, you should be able to look at their documentation and think, Ah, yes, I know what theyre doing, and why theyre doing it.
For OOP developers: Java, Kotlin, Python, and more
Furthermore, Ive written the book especially for people with a background in object-oriented programming (OOP). I taught Java and OOP courses for many years, and still write mostly-OOP code for iPhone and Android apps (using Flutter/Dart and Kotlin, respectively), so Im very familiar with OOP, and what OOP developers need to know to learn FP.
Having worked with Java, Kotlin, Swift, Python, C++, Dart, and Ruby, I believe this book will be helpful to developers who use those languages, and are interested in learning FP.
I accomplish this goal in under 250 pages by focusing on how to write a few small applications with the five main concepts just mentioned, explaining those concepts as we go along.
This is intentionally a small, hopefully easy-to-read book about FP. If youd rather read a large, detailed book on FP, I wrote the best-selling and highly-rated Functional Programming, Simplified, which is over 720 pages long. While its much longer, the benefit of that book is that it does cover a lot of background information and fine details, in the form of over 100 short lessons. (To be clear, the Print version of that book is 720+ pages long, but the PDF and Kindle electronic versions are over 1,000 pages long.)
2. About Me
For those who dont know me, my name is Alvin Alexander, and this is my Java, OOP, Kotlin, Scala, and FP background:
- Back in the day I taught C, Java, and OOP courses to clients like GE, UPS, Papa Johns, and many others
- I founded a little software consulting firm that we grew to 20 employees, and we primarily wrote Java/OOP code for our clients
- I wrote two editions of the Scala Cookbook, most recently the 2nd Edition, which was released in September, 2021 (700+ pages each)
- I wrote the best-selling and highly-rated book, Functional Programming, Simplified (also 700+ pages)
- I wrote a book named Hello, Scala, which became the basis for the Scala Book on the official Scala website
- I then co-wrote the Scala 3 Book for the official Scala website
- In 2022 I released a book named Learn Scala 3 The Fast Way!
- Ive written Android apps with Java and Kotlin, and cross-platform iOS/Android apps with Flutter and Dart
- My website about programming, alvinalexander.com, receives millions of page-views every year
I mention the page counts for some of those books because for me, this is a short book. I intentionally made it as concise as possible, while also writing it in a simple, conversational style, as if I was working right next to you, or mentoring you in a classroom.
3. Welcome!
Welcome! This is a really different book for me. My initial plan was to create a small booklet that I was going to title Thinking with Types or Solving Problems with Pure Functions. The idea was to show how you can write really solid, readable, maintable code using these programming techniques:
- Pure functions
- Immutable variables (algebraic variables)
- Immutable data types (such as
String
and List
) - Expression-oriented programming (EOP)
- Functional error-handling
But then as I started working on the outline and examples, I realized how close this code is to writing pure functional programming (FP) code. At that point I switched from writing a booklet to trying to write the smallest possible book I can that takes OOP developers to the precipice of using Scalas FP libraries.
So even though this is a relatively small book, I hope it delivers a tremendous amount of value by taking you so far down the road to FP. My hope is that when you finish this book, youll be able to go right to the ZIO and Cats Effect documentation and think, Oh, I know what theyre doing, and why theyre doing it.