Functional Thinking
Neal Ford
Preface
The first time I seriously looked at functional programming was in 2004. I became intrigued by alternative languages on the .NET platform, and started playing with Haskell and a few pre-F#, ML-based languages. In 2005, I did a conference talk named Functional Languages and .NET at a few venues, but the languages at the time were more proof-of-concept and toys than anything else. The possibilities of thinking within a new paradigm fascinated me, however, and changed the way I approached some problems in more familiar languages.
I revisited this topic in 2010 because I observed the rise of languages such as Clojure and Scala in the Java ecosystem and remembered the cool stuff from five years before. I started one afternoon on Wikipedia, following link after link, and was mesmerized by the end of the day. That started an exploration of numerous branches of thought in the functional programming world. That research culminated in the Functional Thinking talk, debuting in 2011 at the 33rd Degree Conference in Poland and the IBM developerWorks article series of the same name. Over the course of the next two years, I wrote an article each month on functional programming, which was a great way to establish and maintain a research and exploration plan. I continued delivering (and refining, based on feedback) the presentation until the present day.
This book is the culmination of all the ideas from the Functional Thinking talk and article series. Ive found that the best way to hone material is to present it to audiences over and over, because I learn something new about the material every time I present or write about it. Some relationships and commonalities appear only after deep research and forced thought (deadlines are great focusers!).
My last book, Presentation Patterns , described the importance of visual metaphors in conference presentations. For Functional Thinking , I chose a blackboard and chalk theme (to invoke the mathematical connection to functional programming concepts). At the end of the presentation, as I talk about practical applications, I show a picture of a piece of chalk resting at the foot of a blackboard, metaphorically imploring viewers to pick it up and explore these ideas on their own.
My goal in the talk, the article series, and this book is to present the core ideas of functional programming in a way that is accessible to developers steeped in imperative, object-oriented languages. I hope you enjoy this distillation of ideas, and pick up the chalk and continue your own exploration.
Neal Ford, Atlanta, June 2014
Chapter Overview
Each chapter in this book shows examples of functional thinking. , describes the gradual process of shifting your perspective from that of an object-oriented, imperative programmer to that of a functional programmer. To illustrate the shift in thinking required, I solve a common problem in both imperative and functional styles. I then do an extensive case study, showing the way a functional perspective (and some helper syntax) can help shift you toward a functional mindset.
, shows examples of common chores you can now cede to your language or runtime. One of the moving parts described by Michael Feathers is state , which is typically managed explicitly in nonfunctional languages. Closures allow you to defer some state-handling to the runtime; I show examples of how that state handling mechanism works underneath. In this chapter, I show how functional thinking also allows you to cede details like accumulation to recursion, and impacts your granularity of code reuse.
, of several levels of optimization, both handwritten and via memoization. At the risk of giving away the ending, memoization wins. Lazy data structures, which defer calculation until necessary, allow you to think differently about data structures. I show how to implement lazy data structures (even in nonfunctional languages) and how to leverage laziness that already exists.
, shows how languages are evolving to become more functional. I also talk about evolutionary language trends such as operator overloading and new dispatch options beyond just method calls, about bending your language toward your problem (not the other way around), and common functional data structures such as Option
.
, shows examples of common approaches to problems. I show how design patterns change (or disappear) in the functional programming world. I also contrast code reuse via inheritance versus composition and discuss their explicit and implicit coupling points.
, shows specific long-anticipated functional features that recently appeared in the Java Developer Kit (JDK). I show how Java 8 fits in with the functional thinking from other languages, including the use of higher-order functions (i.e., lambda blocks). I also discuss some clever ways in which Java 8 maintains graceful backward compatibility, and I highlight the Stream
API, which allows concise and descriptive workflows. And, finally, I show how Java 8 has added Option
to eliminate potential null
confusion. I also cover topics such as functional architecture and databases and how the functional perspective changes those designs.
, describes the impact of functional programming on the polyglot world we now live in; we increasingly encounter and incorporate numerous languages on projects. Many new languages are also polyparadigm , supporting several different programming models. For example, Scala supports object-oriented and functional programming. The last chapter discusses the pros and cons of living in a paradigmatically richer world.
Conventions Used in This Book
The following typographical conventions are used in this book:
Italic Indicates 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. Constant width bold
Shows commands or other text that should be typed literally by the user. Constant width italic
Shows text that should be replaced with user-supplied values or by values determined by context.
Note
This icon signifies a tip, suggestion, or general note.
Using Code Examples
Supplemental material (code examples, exercises, etc.) is available for download at https://github.com/oreillymedia/functional_thinking.
This book is here to help you get your job done. In general, if example code is offered with this book, you may use it in your programs and documentation. You do not need to contact us for permission unless youre reproducing a significant portion of the code. For example, writing a program that uses several chunks of code from this book does not require permission. Selling or distributing a CD-ROM of examples from OReilly books does require permission. Answering a question by citing this book and quoting example code does not require permission. Incorporating a significant amount of example code from this book into your products documentation does require permission.
We appreciate, but do not require, attribution. An attribution usually includes the title, author, publisher, and ISBN. For example: Functional Thinking by Neal Ford (OReilly). Copyright 2014 Neal Ford, 978-1-449-36551-6.
If you feel your use of code examples falls outside fair use or the permission given above, feel free to contact us at .