Computing Reviews
Today's Issue Hot Topics Search Browse Recommended My Account Log In
Review Help
Search
Java closures and lambda
Fischer R., Apress, Berkeley, CA, 2015. 220 pp. Type: Book (978-1-430259-98-5)
Date Reviewed: Jul 7 2015

Functional programming and object-oriented programming are inherently incompatible. While the former prohibits any side effects, to the extent of having to dance carefully around input/output (I/O), the latter is predicated on changing state. Still, functional techniques are finding increasing usage in mainstream languages, including C++11 and Java 8. Developers using these languages must therefore learn how to use these techniques effectively. Robert Fischer’s Java closures and lambda was written to serve this purpose for Java programmers.

Functional techniques require functions as first-class objects, which can be passed as parameters, returned from functions and methods, and stored in data structures. In addition, each function must be able to contain references to local variables in the surrounding lexical scope of the function definition. As Fischer points out in the introductory chapter, Java had these all along, in the form of inner classes, and these were used as callback functions in many frameworks. However, the syntax was verbose and cumbersome. Java 8 simplifies the syntax considerably, making functional techniques much easier to use. This is explained in chapter 2. The last chapter complements this high-level view with a discussion of how functions are implemented in bytecode.

The real power of functional techniques, however, is their use to replace loops by series of method calls. Many computations can be composed as series of mapping, filtering, and accumulation operations (which are the basis for the MapReduce programming model). The new Stream interface in Java 8 is an abstraction of a sequence of values that do not necessarily have to be kept in memory simultaneously. A series of Stream operations is therefore only a blueprint for a computation, which will be carried out only when the values are actually required. Java collections have been enhanced to interact well with streams. This topic is the subject of chapter 3.

Existing features of the language require various levels of sophistication in order to work well with streams and functional techniques. Checked exceptions, especially in the context of I/O and resources that need to be closed even when errors occur, are probably the most problematic. Chapters 4 and 7 explain the issues and provide solutions. Working with databases using functional techniques requires a conversion of ResultSet objects to streams, as explained in chapter 5, with another detailed example given in the appendix.

Perhaps the biggest benefit of using streams in Java 8 is their ability to perform computations in parallel. Not only is the code much shorter than the equivalent sequential code, it is also more efficient, and with little effort from the programmer. The inherent mismatch between object-oriented and functional programming manifests itself particularly when the order of computation is changed (as is the case when the code is parallelized). This requires some care, and certain streams cannot be used in parallel mode for that reason. Chapter 6 discusses the concurrent properties of streams.

There are many books about functional programming. Some are more theoretical, others more practical; some use pure functional languages such as Haskell, others use scripting languages such as JavaScript, and some, like this one, use object-oriented languages. Because the implementation of functional techniques in mainstream languages is different for each language (and, significantly, the libraries), what works for one language does not always carry over to others. Each technique may be a better or worse fit for a specific language. Fischer’s focus is how functional programming fits into Java 8.

This book is rather weak on the theory, and some passages, such as the explanation of currying, or the summary of “other programming paradigms” in the appendix, are more confusing than helpful. The book also has quite a few typographical errors, which are sometimes baffling and sometimes misleading. This is more than made up for by the treatment of how to use functional techniques in Java 8 in the main part of the book. This is presented very clearly, with detailed nontrivial yet not overly complex examples. Often, there are different ways to address an issue; Fischer provides alternatives, with clear explanations of the relative strengths and weaknesses of each.

Fischer presents many best practices, including things that are not obvious from the documentation. Examples include discussions of the need to separate synchronous and asynchronous behaviors, how to combine functional and object-oriented techniques for maintainability by putting functions inside the relevant classes, and how to make interfaces with two or more methods lambda-friendly using default methods. How to avoid (and check for) null values is shown throughout the book. Some topics are treated rather briefly; I would have liked to see more detailed discussions of topics such as spliterators and fork/join pools.

In summary, this book is an excellent starting point for Java programmers who want to make the most of the new functional techniques in Java 8.

More reviews about this item: Amazon

Reviewer:  Yishai Feldman Review #: CR143584 (1509-0747)
Bookmark and Share
  Reviewer Selected
Featured Reviewer
 
 
Applicative (Functional) Programming (D.1.1 )
 
 
Java (D.3.2 ... )
 
 
Object-Oriented Programming (D.1.5 )
 
Would you recommend this review?
yes
no
Other reviews under "Applicative (Functional) Programming": Date
Functional programming with Hope
Bailey R., Ellis Horwood, Upper Saddle River, NJ, 1990. Type: Book (9780133382372)
May 1 1992
Prospects for functional programming in software engineering
Banâtre J., Jones S., Le Métayer D. (ed), Springer-Verlag New York, Inc., New York, NY, 1991. Type: Book (9780387538525)
Aug 1 1992
An introduction to functional programming
Bird R. (ed), Wadler P., Prentice Hall International (UK) Ltd., Hertfordshire, UK, 1988. Type: Book (9780134841892)
May 1 1992
more...

E-Mail This Printer-Friendly
Send Your Comments
Contact Us
Reproduction in whole or in part without permission is prohibited.   Copyright 1999-2024 ThinkLoud®
Terms of Use
| Privacy Policy