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 you’re using Java and want to see how functional programming can help you work more efficiently, or if you’ve started using Scala and Clojure and can’t quite wrap your head around functional problem-solving, this is the book for you.

Before we dig in, I’d 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 isn’t 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 don’t know what the changes are and we can’t 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. “How’s it going?” he asks. “Working on it,” I say. Ten minutes later, my grandboss pokes his head into my office. “How’s it going?” he asks. “Working on it,” I say. Another ten minutes pass by when my great-grandboss pokes his head into my office. “How’s 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 what’s 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 wasn’t Java.”

The REPL, Clojure’s interactive programming environment, helped a lot in this story. However, lots of languages that aren’t particularly functional have similar interactive programming environments, so that’s not all there is to it.

Two of the patterns that we’ll see in this book, Pattern 21, Domain-Specific Language, and Pattern 15, Chain of Operations, contributed greatly to this story’s 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 they’re 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 we’ll see in Pattern 15, Chain of Operations, helped me quickly write code to clean up the mess.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.175.253