Java programming with lambda expressions

Java programming with lambda expressions.

In the technical keynote address for JavaOne 2013, Mark Reinhold, chief architect for the Java Platform Group at Oracle, described lambda expressions as the single largest upgrade to the Java programming model ever. While there are many applications for lambda expressions, this article focuses on a specific example that occurs frequently in mathematical applications; namely, the need to pass a function to an algorithm.

As a gray-haired geek, I have programmed in numerous languages over the years, and I have programmed extensively in Java since version 1.1. When I started working with computers, almost no one had a degree in computer science. Computer professionals came mostly from other disciplines such as electrical engineering, physics, business, and mathematics. In my own former life I was a mathematician, and so it should come as no surprise that my initial view of a computer was that of a giant programmable calculator. I've broadened my view of computers considerably over the years, but I still welcome the opportunity to work on applications that involve some aspect of mathematics.

Many applications in mathematics require that a function be passed as a parameter to an algorithm. Examples from college algebra and basic calculus include solving an equation or computing the integral of a function. For over 15 years Java has been my programming language of choice for most applications, but it was the first language that I used on a frequent basis that did not allow me to pass a function (technically a pointer or reference to a function) as a parameter in a simple, straightforward manner. That shortcoming is about to change with the upcoming release of Java 8.

The power of lambda expressions extends well beyond a single use case, but studying various implementations of the same example should leave you with a solid sense of how lambdas will benefit your Java programs. In this article I will use a common example to help describe the problem, then provide solutions written in C++, Java before lambda expressions, and Java with lambda expressions. Note that a strong background in mathematics is not required to understand and appreciate the major points of this article.

Lambda expressions in a mathematical example

The example used throughout this article is Simpson's Rule from basic calculus. Simpson's Rule, or more specifically Composite Simpson's Rule, is a numerical integration technique to approximate a definite integral. Don't worry if you are unfamiliar with the concept of a definite integral; what you really need to understand is that Simpson's Rule is an algorithm that computes a real number based on four parameters:

  • A function that we want to integrate.
  • Two real numbers a and b that represent the endpoints of an interval [a,b] on the real number line. (Note that the function referred to above should be continuous on this interval.)
  • An even integer n that specifies a number of subintervals. In implementing Simpson's Rule we divide the interval [a,b] into n subintervals.

To simplify the presentation, let's focus on the programming interface and not on the implementation details. (Truthfully, I hope that this approach will let us bypass arguments about the best or most efficient way to implement Simpson's Rule, which is not the focus of this article.) We will use type double for parameters a and b, and we will use type int for parameter n. The function to be integrated will take a single parameter of type double and a return a value of type double.

Download

Created by John I. Moore for JavaWorld

Function parameters in C++

To provide a basis for comparison, let's start with a C++ specification. When passing a function as a parameter in C++, I usually prefer to specify the signature of the function parameter using a typedef. Listing 1 shows a C++ header file named simpson.h that specifies both the typedef for the function parameter and the programming interface for a C++ function named integrate. The function body for integrate is contained in a C++ source code file named simpson.cpp (not shown) and provides the implementation for Simpson's Rule.

Listing 1. C++ header file for Simpson's Rule

    #if !defined(SIMPSON_H)  #define SIMPSON_H  #include <stdexcept>  using namespace std;  typedef double DoubleFunction(double x);  double integrate(DoubleFunction f, double a, double b, int n)      throw(invalid_argument);  


Related Posts To Java programming with lambda expressions


Java programming with lambda expressions Rating: 4.5 Posted by: Brot Trune

Search Here

Popular Posts

Total Pageviews

Recent Posts