Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.

Channels ▼

Developer Reading List

, February 25, 2014 Java 8, JavaScript, Functional Programming, and Software Engineering
  • E-mail
  • Print

Functional Programming Patterns in Scala and Clojure

by Michael Bevilacqua-Linn

Functional programming is a style that is wildly popular in academia, but substantially absent in business programming. Part of the reason for this situration is that, to migrate to the functional way of thinking, you need to relearn many fundamental techniques — from simple looping, to handling collections, even how you think about simple problems. Most experienced developers just don't have the bandwidth to relearn their craft from top to bottom for the small and, at times, hypothetical lift that functional programming will provide. As a result, they most often admire it at a distance and continue working with the familiar imperative and OO styles they know.

The author of this work contends that on the JVM, at least, there are plenty of opportunities to improve OO code by selective integration of functional techniques. (This is an approach implicit in the multi-paradigm model embraced by Scala.) The points he focuses on are traditional design patterns; and he suggests ways of replacing them in existing code with functional substitutes. The benefit is less code, better code quality, and a greater amenability to parallel execution.

As a working example, he writes a micro Web framework in Java that uses MVC. He then rewrites it in Scala and Clojure. From there, he devotes a chapter each to rewriting the patterns found in the exercise using a functional approach. These patterns include the command pattern, builders, iterators, the template method, null object pattern, and the strategy, decorator, and visitor patterns.

He then elaborates functional patterns for thoughtful addition to existing OO programs: tail recursion, filter-map-reduce, chain of operations, memoization, and others.

The result is an excellent and deep exploration of programming concepts. Functional Programming Patterns in Scala and Clojure is hard-going because of the density of the material and the amount of knowledge the author expects from the reader. But for developers who know their patterns, have thought carefully about program design, and can work through the Scala examples (which are close enough to Java and C++ to be followed satisfactorily), this book is likely to change the way they write code significantly for the better. Highly recommended.

Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.