English 中文(简体)
Functions
  • 时间:2024-12-22

Functional Programming with Java - Functions


Previous Page Next Page  

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 usabipty. 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 pke 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 −

pubpc class FunctionTester {
   pubpc 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

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 functionapty.

import java.util.function.Function;

pubpc class FunctionTester {
   pubpc 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

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.

pubpc class FunctionTester {
   pubpc 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

3
Advertisements