Aspects

Java 8 Onwards

Functional Programming

Streams

Useful Resources

Functional Programming - Functions



A function is a block of statements that performs a specific task. Functions accept data, process it, and return a result. Functions are written primarily to support the concept of re usability. Once a function is written, it can be called easily, without having to write the same code again and again.

Functional Programming revolves around first class functions, pure functions and high order functions.

  • A First Class Function is the one that uses first class entities like String, numbers which can be passed as arguments, can be returned or assigned to a variable.

  • A High Order Function is the one which can take a function as an argument and/or can return a function.

  • A Pure Function is the one which has no side effect while its execution.

First Class Function

A first class function can be treated as a variable. That means it can be passed as a parameter to a function, it can be returned by a function or can be assigned to a variable as well. Java supports first class function using lambda expression. A lambda expression is analogous to an anonymous function. See the example below −

FunctionTester.java

package com.tutorialspoint;

public class FunctionTester {
   public static void main(String[] args) {
      int[] array = {1, 2, 3, 4, 5};
      SquareMaker squareMaker = item -> item * item;
      for(int i = 0; i < array.length; i++){
         System.out.println(squareMaker.square(array[i]));
      }
   }
}
interface SquareMaker {
   int square(int item);
}

Output

Run the FunctionTester and verify the output.

1
4
9
16
25

Here we have created the implementation of square function using a lambda expression and assigned it to variable squareMaker.

High Order Function

A high order function either takes a function as a parameter or returns a function. In Java, we can pass or return a lambda expression to achieve such functionality.

FunctionTester.java

package com.tutorialspoint;

import java.util.function.Function;

public class FunctionTester {
   public static void main(String[] args) {
      int[] array = {1, 2, 3, 4, 5};

      Function<Integer, Integer> square = t -> t * t;        
      Function<Integer, Integer> cube = t -> t * t * t;

      for(int i = 0; i < array.length; i++){
         print(square, array[i]);
      }        
      for(int i = 0; i < array.length; i++){
         print(cube, array[i]);
      }
   }

   private static <T, R> void print(Function<T, R> function, T t ) {
      System.out.println(function.apply(t));
   }
}

Output

Run the FunctionTester and verify the output.

1
4
9
16
25
1
8
27
64
125

Pure Function

A pure function does not modify any global variable or modify any reference passed as a parameter to it. So it has no side-effect. It always returns the same value when invoked with same parameters. Such functions are very useful and are thread safe. In example below, sum is a pure function.

FunctionTester.java

package com.tutorialspoint;

public class FunctionTester {
   public static void main(String[] args) {
      int a, b;
      a = 1;
      b = 2;
      System.out.println(sum(a, b));
   }

   private static int sum(int a, int b){
      return a + b;
   }
}

Output

Run the FunctionTester and verify the output.

3
Advertisements