Functional Programming Patterns in Scala and Clojure
Write Lean Programs for the JVM
by Michael Bevilacqua-Linn
Version: P1.0 (October 2013)
Copyright 2013 The Pragmatic Programmers, LLC. This book is licensed tothe individual who purchased it. We don't copy-protect itbecause that would limit your ability to use it for yourown purposes. Please don't break this trustyou can use this across all of your devices but please do not share this copywith other members of your team, with friends, or via file sharing services. Thanks.
Dave & Andy.
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and The Pragmatic Programmers, LLC was aware of a trademark claim, the designations have been printed in initial capital letters or in all capitals. The Pragmatic Starter Kit, The Pragmatic Programmer, Pragmatic Programming, Pragmatic Bookshelf and the linking g device are trademarks of The Pragmatic Programmers, LLC.
Every precaution was taken in the preparation of this book. However, the publisher assumes no responsibility for errors or omissions, or for damages that may result from the use of information (including program listings) contained herein.
Our Pragmatic courses, workshops, and other products can help you and your team create better software and have more fun. For more information, as well as the latest Pragmatic titles, please visit us at http://pragprog.com.
Table of Contents
Copyright 2013, The Pragmatic Bookshelf.
Early Praise for Functional Programming Patterns
This book is an absolute gem and should be required reading for anybody looking to transition from OO to FP. It is an extremely well-built safety rope for those crossing the bridge between two very different worlds. Consider this mandatory reading.
Colin Yates, technical team leader at QFI Consulting, LLP |
This book sticks to the meat and potatoes of what functional programming can do for the object-oriented JVM programmer. The functional patterns are sectioned in the back of the book separate from the functional replacements of the object-oriented patterns, making the book handy reference material. As a Scala programmer, I even picked up some new tricks along the read.
Justin James, developer with Full Stack Apps |
This book is good for those who have dabbled a bit in Clojure or Scala but are not really comfortable with it; the ideal audience is seasoned OO programmers looking to adopt a functional style, as it gives those programmers a guide for transitioning away from the patterns they are comfortable with.
Rod Hilton, Java developer and PhD candidate at the University of Colorado |
Acknowledgments
Id like to thank my parents, without whom I would not exist.
Thanks also go to my wonderful girlfriend, who put up with many a night and weekend listening to me mutter about code samples, inconsistent tenses, and run-on sentences.
This book would have suffered greatly without a great group of technical reviewers. My thanks to Rod Hilton, Michajlo Mishu Matijkiw, Venkat Subramaniam, Justin James, Dave Cleaver, Ted Neward, Neal Ford, Richard Minerich, Dustin Campbell, Dave Copeland, Josh Carter, Fred Daoud, and Chris Smith.
Finally, Id like to thank Dave Thomas and Andy Hunt. Their book, The Pragmatic Programmer, is one of the first books I read when I started my career. It made a tremendous impact, and Ive still got my original dog-eared, fingerprint-covered, bruised and battered copy. In the Pragmatic Bookshelf, theyve created a publisher thats truly dedicated to producing high-quality technical books and supporting the authors who write them.
Copyright 2013, The Pragmatic Bookshelf.
Preface
This book is about patterns and functional programming in Scala and Clojure. It shows how to replace, or greatly simplify, many of the common patterns we use in object-oriented programming, and it introduces some patterns commonly used in the functional world.
Used together, these patterns let programmers solve problems faster and in a more concise, declarative style than with object-oriented programming alone. If youre using Java and want to see how functional programming can help you work more efficiently, or if youve started using Scala and Clojure and cant quite wrap your head around functional problem-solving, this is the book for you.
Before we dig in, Id like to start off with a story. This story is true, though some names have been changed to protect the not-so-innocent.
A Tale of Functional Programming
by Michael Bevilacqua-Linn, software firefighter
Michael Bevilacqua-Linn
The site isnt down, but an awful lot of alarms are going off. We trace the problems to changes someone made to a third-party API we use. The changes are causing major data problems on our side; namely, we dont know what the changes are and we cant find anyone who can tell us. It also turns out the system that talks to the API uses legacy code, and the only guy who knows how to work on it happens to be away on vacation. This a big system: 500,000-lines-of-Java-and-OSGI big.
Support calls are flooding in, lots of them. Expensive support calls from frustrated customers. We need to fix the problem quickly. I start up a Clojure REPL and use it to poke around the problem API.
My boss pokes his head into my office. Hows it going? he asks. Working on it, I say. Ten minutes later, my grandboss pokes his head into my office. Hows it going? he asks. Working on it, I say. Another ten minutes pass by when my great-grandboss pokes his head into my office. Hows it going? he asks. Working on it, I say. I get a half hour of silence before the CTO pokes his head into my office. Working on it, I say before he opens his mouth.
An hour passes, and I figure out whats changed. I whip up a way to keep the data clean until the legacy developer gets back and can put together a proper fix. I hand my little program off to the operations team, which gets it up and running in a JVM, somewhere safe. The support calls stop coming in, and everyone relaxes a bit.
A week or so later at an all-hands meeting, the great-grandboss thanks me for the Java program I wrote that saved the day. I smile and say, That wasnt Java.
The REPL, Clojures interactive programming environment, helped a lot in this story. However, lots of languages that arent particularly functional have similar interactive programming environments, so thats not all there is to it.
Two of the patterns that well see in this book, Pattern 21, , contributed greatly to this storys happy ending.
Earlier on, I had written a small instance of domain-specific language for working with these particular APIs that helped me explore them very quickly even though theyre very large and it was difficult to figure out where the problem might lie. In addition, the powerful data transformation facilities that functional programming relies on, such as the examples well see in Pattern 15, , helped me quickly write code to clean up the mess.
How This Book Is Organized
Well start with an introduction to patterns and how they relate to functional programming. Then well take a look at an extended example, a small web framework called TinyWeb. Well first show TinyWeb written using classic object-oriented patterns in Java. Well then rewrite it, piece by piece, to a hybrid style that is object oriented and functional, using Scala. Well then write in a functional style using Clojure.