Java 101: Functional programming for Java developers, Part 1

Optimize your Java code with these five functional programming techniques

1 2 Page 2
Page 2 of 2

Suppose calculatebonus2() was refactored to return calculatebonus(numSales). Would calculatebonus2() still be pure? The answer is no: when a pure function invokes an impure function, the "pure function" becomes impure.

When no data dependency exists between pure functions, they can be evaluated in any order without affecting the outcome, making them suitable for parallel execution. This is one of functional programming's benefits.

Functional programming with higher-order functions

A higher-order function is a mathematical function that receives functions as arguments, returns a function to its caller, or both. One example is calculus's differential operator, d/dx, which returns the derivative of function f.

The JavaScript in Listing 4 demonstrates passing anonymous comparison functions to a first-class sorting function.

Listing 4. Passing anonymous comparison functions (script2.js)



function sort(a, cmp)

{

   for (var pass = 0; pass < a.length - 1; pass++)

      for (var i = a.length - 1; i > pass; i--)

         if (cmp(a[i], a[pass]) < 0)

         {

            var temp = a[i]

            a[i] = a[pass]

            a[pass] = temp

         }

}

var a = [22, 91, 3, 45, 64, 67, -1]

sort(a, function(i, j)

        {

           return i - j;

        })

a.forEach(function(entry) { print(entry) })

print('\n')

sort(a, function(i, j)

        {

           return j - i;

        })

a.forEach(function(entry) { print(entry) })

print('\n')

a = ["X", "E", "Q", "A", "P"]

sort(a, function(i, j)

        {

           return i < j ? -1 : i > j;

        })

a.forEach(function(entry) { print(entry) })

print('\n')

sort(a, function(i, j)

        {

           return i > j ? -1 : i < j;

        })

a.forEach(function(entry) { print(entry) })

In this example, the initial sort() call receives an array as its first argument, followed by an anonymous comparison function. When called, the anonymous comparison function executes return i - j; to achieve an ascending sort. By reversing i and j, the second comparison function achieves a descending sort. The third and fourth sort() calls receive anonymous comparison functions that are slightly different in order to properly compare string values.

Run the script2.js example as follows:



java RunScript script2.js

Here's the expected output:



-1

3

22

45

64

67

91

91

67

64

45

22

3

-1

A

E

P

Q

X

X

Q

P

E

A

Filter and map

Functional programming languages typically provide several useful higher-order functions. Two common examples are filter and map.

  • A filter processes a list in some order to produce a new list containing exactly those elements of the original list for which a given predicate (think Boolean expression) returns true.
  • A map applies a given function to each element of a list, returning a list of results in the same order.

JavaScript supports filtering and mapping functionality via the filter() and map() higher-order functions. Listing 5 demonstrates these functions for filtering out odd numbers and mapping numbers to their cubes.

Listing 5. Filtering and mapping (script3.js)



print([1, 2, 3, 4, 5, 6].filter(function(num) { return num % 2 == 0 }))

print('\n')

print([3, 13, 22].map(function(num) { return num * 3 }))

Run the script3.js example as follows:



java RunScript script3.js

You should observe the following output:



2,4,6

9,39,66

Reduce

Another common higher-order function is reduce, which is more commonly known as a fold. This function reduces a list to a single value.

Listing 6 uses JavaScript's reduce() higher-order function to reduce an array of numbers to a single number, which is then divided by the array's length to obtain an average.

Listing 6. Reducing an array of numbers to a single number (script4.js)



var numbers = [22, 30, 43]

print(numbers.reduce(function(acc, curval) { return acc + curval })

      / numbers.length)

Run Listing 6's script (in script4.js) as follows:



java RunScript script4.js

You should observe the following output:



31.666666666666668

You might think that the filter, map, and reduce higher-order functions obviate the need for if-else and various looping statements, and you would be right. Their internal implementations take care of decisions and iteration.

A higher-order function uses recursion to achieve iteration. A recursive function invokes itself, allowing an operation to repeat until it reaches a base case. You can also leverage recursion to achieve iteration in your functional code.

Functional programming with lazy evaluation

Another important functional programming feature is lazy evaluation (also known as nonstrict evaluation), which is the deferral of expression evaluation for as long as possible. Lazy evaluation offers several benefits, including these two:

  • Expensive (timewise) calculations can be deferred until they're absolutely necessary.
  • Unbounded collections are possible. They'll keep delivering elements for as long as they're requested to do so.

Lazy evaluation is integral to Haskell. It won't calculate anything (including a function's arguments before the function is called) unless it's strictly necessary to do so.

Java's Streams API capitalizes on lazy evaluation. A stream's intermediate operations (e.g., filter()) are always lazy; they don't do anything until a terminal operation (e.g., forEach()) is executed.

Although lazy evaluation is an important part of functional languages, even many imperative languages provide builtin support for some forms of laziness. For example, most programming languages support short-circuit evaluation in the context of the Boolean AND and OR operators. These operators are lazy, refusing to evaluate their right-hand operands when the left-hand operand is false (AND) or true (OR).

Listing 7 is an example of lazy evaluation in a JavaScript script.

Listing 7. Lazy evaluation in JavaScript (script5.js)



var a = false && expensiveFunction("1")

var b = true && expensiveFunction("2")

var c = false || expensiveFunction("3")

var d = true || expensiveFunction("4")

function expensiveFunction(id)

{

   print("expensiveFunction() called with " + id)

}

Run the code in script5.js as follows:



java RunScript script5.js

You should observe the following output:



expensiveFunction() called with 2

expensiveFunction() called with 3

Lazy evaluation is often combined with memoization, an optimization technique used primarily to speed up computer programs by storing the results of expensive function calls and returning the cached result when the same inputs reoccur.

Because lazy evaluation doesn't work with side effects (such as code that produces exceptions and I/O), imperative languages mainly use eager evaluation (also known as strict evaluation), where an expression is evaluated as soon as it's bound to a variable.

Functional programming with closures

First-class functions are associated with the concept of a closure, which is a persistent scope that holds onto local variables even after the code execution has left the block in which the local variables were defined.

To help clarify this concept, Listing 8 presents a JavaScript script that introduces a simple closure. The script is based on the example presented here.

Listing 8. A simple closure (script6.js)



function add(x)

{

   function partialAdd(y)

   {

      return y + x

   }

   return partialAdd

}

var add10 = add(10)

var add20 = add(20)

print(add10(5))

print(add20(5))

Listing 8 defines a first-class function named add() with a parameter x and a nested function partialAdd(). The nested function partialAdd() has access to x because x is in add()'s lexical scope. Function add() returns a closure that contains a reference to partialAdd() and a copy of the environment around add(), in which x has the value assigned to it in a specific invocation of add().

Because add() returns a value of function type, variables add10 and add20 also have function type. The add10(5) invocation returns 15 because the invocation assigns 5 to parameter y in the call to partialAdd(), using the saved environment for partialAdd() where x is 10. The add20(5) invocation returns 25 because, although it also assigns 5 to y in the call to partialAdd(), it's now using another saved environment for partialAdd() where x is 20. Thus, while add10() and add20() use the same function partialAdd(), the associated environments differ and invoking the closures will bind x to two different values in the two invocations, evaluating the function to two different results.

Run Listing 8's script (in script6.js) as follows:



java RunScript script6.js

You should observe the following output:



15

25

Functional programming with currying

Currying is a way to translate the evaluation of a multi-argument function into the evaluation of an equivalent sequence of single-argument functions. For example, a function takes two arguments: x and y. Currying transforms the function into taking only x and returning a function that takes only y. Currying is related to but is not the same as partial application, which is the process of fixing a number of arguments to a function, producing another function of smaller arity.

Listing 9 presents a JavaScript script that demonstrates currying.

Listing 9. Currying in JavaScript (script7.js)



function multiply(x, y)

{

   return x * y

}

function curried_multiply(x)

{

   return function(y)

   {

      return x * y

   }

}

print(multiply(6, 7))

print(curried_multiply(6)(7))

var mul_by_4 = curried_multiply(4)

print(mul_by_4(2))

The script presents a noncurried two-argument multiply() function, followed by a first-class curried_multiply() function that receives multiplicand argument x and returns a closure containing a reference to an anonymous function (that receives multiplier argument y) and a copy of the environment around curried_multiply(), in which x has the value assigned to it in an invocation of curried_multiply().

The rest of the script first invokes multiply() with two arguments and prints the result. It then invokes curried_multiply() in two ways:

  • curried_multiply(6)(7) results in curried_multiply(6) executing first. The returned closure executes the anonymous function with the closure's saved x value 6 being multiplied by 7.
  • var mul_by_4 = curried_multiply(4) executes curried_multiply(4) and assigns the closure to mul_by_4. mul_by_4(2) executes the anonymous function with the closure's 4 value and the passed argument 2.

Run Listing 9's script (in script7.js) as follows:



java RunScript script7.js

You should observe the following output:



42

42

8

In conclusion

In this tutorial you've learned some basics of functional programming. We've used examples in JavaScript to study five core functional programming techniques, which we'll further explore using Java code in Part 2. In addition to touring Java 8's functional programming capabilities, the second half of this tutorial will help you begin to think functionally, by converting an example of object-oriented Java code to its functional equivalent.

Learn more about functional programming

I found the book Introduction to Functional Programming (Richard Bird and Philip Wadler, Prentice Hall International Series in Computing Science, 1992) helpful in learning the basics of functional programming.

1 2 Page 2
Page 2 of 2