Learning Functional Programming
by Jack Widman , PhD
Copyright 2022 Jack Widman. All rights reserved.
Printed in the United States of America.
Published by OReilly Media, Inc. , 1005 Gravenstein Highway North, Sebastopol, CA 95472.
OReilly books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles (http://oreilly.com). For more information, contact our corporate/institutional sales department: 800-998-9938 or corporate@oreilly.com .
- Acquisitions Editor: Mary Preap
- Development Editor: Shira Evans
- Production Editor: Kate Galloway
- Copyeditor: Justin Billing
- Proofreader: Stephanie English
- Indexer: nSight, Inc.
- Interior Designer: David Futato
- Cover Designer: Karen Montgomery
- Illustrator: Kate Dullea
- August 2022: First Edition
Revision History for the First Edition
- 2022-08-11: First Release
See http://oreilly.com/catalog/errata.csp?isbn=9781098111755 for release details.
The OReilly logo is a registered trademark of OReilly Media, Inc. Learning Functional Programming, the cover image, and related trade dress are trademarks of OReilly Media, Inc.
The views expressed in this work are those of the author, and do not represent the publishers views. While the publisher and the author have used good faith efforts to ensure that the information and instructions contained in this work are accurate, the publisher and the author disclaim all responsibility for errors or omissions, including without limitation responsibility for damages resulting from the use of or reliance on this work. Use of the information and instructions contained in this work is at your own risk. If any code samples or other technology this work contains or describes is subject to open source licenses or the intellectual property rights of others, it is your responsibility to ensure that your use thereof complies with such licenses and/or rights.
978-1-098-11175-5
[LSI]
Dedication
To my three wonderful daughters: Katherine, Annie, and Victoria. And to Andrea, whose support and love have encouraged me in so many ways.
Preface
Over the past few years, functional programming (FP) has been experiencing a renaissance. Many companies are looking for programmers with experience in FP, as languages that were not originally designed to be functional have evolved over time to include functional capabilities: languages such as Java, JavaScript, and Python, to name a few. The push for programmers with functional experience is due, in part, to a perceived improvement in the development process, including a sense that fewer bugs are produced and more extensible and robust code is produced when following the functional way. Whether this is true or notand whether a greater percentage of the code written in the coming years is functionalwill become evident in time. For now, let us consider FP one of a number of paradigms, each with its own pros and cons.
Who Should Use This Book?
Essentially, all programmers. If you have no experience in FP, but have heard about it and you are curious, or even if you picked up this book in a store without any knowledge of FP, this book will prove useful to you. Experienced FP programmers too, will find something to benefit them. The book dives into the category theory roots of FP in a way not presented in other books on this subject. Finally, programmers with some experience using FP but who want to gain a more advanced understanding of the concepts and theory that make up FP will find much to use and enjoy.
How The Book Is Organized
I endeavor to demonstrate, through various programming languages, how functional constructs can improve our code. However, you will notice that Scala is the frequent language of choice for code examples due to the ease with which functional ideas can be expressed in Scala; the reader will more easily appreciate and understand the functional ideas when they are expressed in the natural way, which Scala allows. For a mini-introduction to Scala, see the .
Conventions Used in This Book
The following typographical conventions are used in this book:
ItalicIndicates new terms, URLs, email addresses, filenames, and file extensions.
Constant width
Used for program listings, as well as within paragraphs to refer to program elements such as variable or function names, databases, data types, environment variables, statements, and keywords.
Tip
This element signifies a tip or suggestion.
Note
This element signifies a general note.
Warning
This element indicates a warning or caution.
OReilly Online Learning
Note
For more than 40 years, OReilly Media has provided technology and business training, knowledge, and insight to help companies succeed.
Our unique network of experts and innovators share their knowledge and expertise through books, articles, and our online learning platform. OReillys online learning platform gives you on-demand access to live training courses, in-depth learning paths, interactive coding environments, and a vast collection of text and video from OReilly and 200+ other publishers. For more information, visit http://oreilly.com.
How to Contact Us
Please address comments and questions concerning this book to the publisher:
- OReilly Media, Inc.
- 1005 Gravenstein Highway North
- Sebastopol, CA 95472
- 800-998-9938 (in the United States or Canada)
- 707-829-0515 (international or local)
- 707-829-0104 (fax)
We have a web page for this book, where we list errata, examples, and any additional information. You can access this page at https://oreil.ly/learning-fp.
Email to comment or ask technical questions about this book.
For news and information about our books and courses, visit https://oreilly.com.
Find us on LinkedIn: https://linkedin.com/company/oreilly-media.
Follow us on Twitter: https://twitter.com/oreillymedia.
Watch us on YouTube: https://youtube.com/oreillymedia.
Chapter 1. What Is Functional Programming?
Functional programming? Functors? Monoids? Monads? Im not a mathematician! you might say. How can I learn these esoteric concepts? And why would I want to? These concerns are totally understandable. But the truth is you dont need to be a mathematician to be a functional programmer.
The fundamental concepts of FP are easy to understand when presented in a clear, straightforward way. And that is what this book is about. Making FP understandable and practical. In particular, I will teach you how to think like a functional programmer. But why would you want to learn FP?
Picture this. Its 10 p.m. and you are totally stuck while trying to fix a bug in a program you need to submit in the morning. The problem seems to be centered around a variable called ratio. The problem is that depending on the state of the system you are modeling, the variable ratio keeps changing. Your frustration builds. Or you have a deadline at work and there is an elusive bug in your microservice that you are chasing down. The problem seems to be in two nested for loops in which variables are modified in a fairly complex way. The logic is complex and you dont quite see the solution. If only there were a way to write programs in a way in which the value of variables would not change! FP to the rescue.