Functional Programming with Java - 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 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 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.
A Pure Function is the one which has no side effect while its execution.
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 −
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);
}
1
4
9
16
25

Here we have created the implementation of square function using a lambda expression and assigned it to variable squareMaker.
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.
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));
   }
}
1
4
9
16
25
1
8
27
64
125

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.
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;
   }
}
3


    32 Lectures 
     3.5 hours 

 Pavan Lalwani

    11 Lectures 
     1 hours 

 Prof. Paul Cline, Ed.D

    72 Lectures 
     10.5 hours 

 Arun Ammasai

    51 Lectures 
     2 hours 

 Skillbakerystudios

    43 Lectures 
     4 hours 

 Mohammad Nauman

    8 Lectures 
     1 hours 

 Santharam Sivalenka
 Print
 Add Notes
 Bookmark this page