Functional Programming in JAVA
JAVA 8 release is the package of all surprises, functional programming is one among them.
Let's explore a bit on functional programming style in java...
Functional programming is the natural successor to Object Oriented programming.The objective of the design of the FPL is to mimic mathematical functions to the greatest extent possible.
Will start with examples for functional programming in java
Normally if we want to write any functions say addition of two numbers we were following below approach:
public int add(int x,int y)
{
return x+y;
}
The above piece of code can be written in FP style like
Function<Integer,Integer> adder = (x,y)->x+y;
Where Function belongs to the package java.util.function
General prototype
java.util.function.Function<T,R>
where T is input datatype, R is return data type.
Java has one more Interface called BinaryOperator under java.util.function which will be used to define function which takes two inputs of same type and returns result of same type as input parameter. So by making use of BinaryOperator above code can be written as
BinaryOperator<Integer> sum = (a,b) -> a + b;
These functions can be called through apply method as follows:
Integer int res1 = adder.apply(1,2); //returns 3
Integer int res2 = sum.apply(1,2); //returns 3
Similarly we have UnaryOperator Interface under java.util.function which is used to define operation which takes single input and produces result of same type as input
Function<Integer,Integer> concat = x -> x + 1;
System.out.println("Result: "+concat.apply(12)); //Prints 13
Using java.util.function.UnaryOperator above can be written as:
UnaryOperator<Integer> uOp = x->x+1;
System.out.println("Unary Result :"+uOp.apply(12)); //Prints 13
The above example are called simple order functions. Just imagine the situation where we pass functions as a parameter to some other function and execute. Confusing isn't it? Yeah! initially I felt the same.
Well what comes to your mind while seeing f o g = f(g(x))? Yea you guessed it right it is "Function Composition"
Composite Function approach can be attained in Java using lambda expression as follows:
//Composite order function
Function<Integer,Function<Integer,Integer>> fun= x->y->y+x;
System.out.println("Result 2: "+fun.apply(4).apply(2));//Prints 6
Let's write program to find nth Fibonacci number using FP
Function<Integer,Integer> fib = x-> x<=1?1:Sample.fib.apply(x-2)+Sample.fib.apply(x-1);
System.out.println("5th fib no."+fib.apply(5)); //Prints 8
Few other examples where we can use functional programming
//Function which increments a numerical string value by one
Function<String,String>incStr=s->String.valueOf (Integer.parseInt(s)
+1);
Let's explore a bit on functional programming style in java...
Functional programming is the natural successor to Object Oriented programming.The objective of the design of the FPL is to mimic mathematical functions to the greatest extent possible.
Will start with examples for functional programming in java
Normally if we want to write any functions say addition of two numbers we were following below approach:
public int add(int x,int y)
{
return x+y;
}
The above piece of code can be written in FP style like
Function<Integer,Integer> adder = (x,y)->x+y;
Where Function belongs to the package java.util.function
General prototype
java.util.function.Function<T,R>
where T is input datatype, R is return data type.
Java has one more Interface called BinaryOperator under java.util.function which will be used to define function which takes two inputs of same type and returns result of same type as input parameter. So by making use of BinaryOperator above code can be written as
BinaryOperator<Integer> sum = (a,b) -> a + b;
These functions can be called through apply method as follows:
Integer int res1 = adder.apply(1,2); //returns 3
Integer int res2 = sum.apply(1,2); //returns 3
Similarly we have UnaryOperator Interface under java.util.function which is used to define operation which takes single input and produces result of same type as input
Function<Integer,Integer> concat = x -> x + 1;
System.out.println("Result: "+concat.apply(12)); //Prints 13
Using java.util.function.UnaryOperator above can be written as:
UnaryOperator<Integer> uOp = x->x+1;
System.out.println("Unary Result :"+uOp.apply(12)); //Prints 13
The above example are called simple order functions. Just imagine the situation where we pass functions as a parameter to some other function and execute. Confusing isn't it? Yeah! initially I felt the same.
Well what comes to your mind while seeing f o g = f(g(x))? Yea you guessed it right it is "Function Composition"
Composite Function approach can be attained in Java using lambda expression as follows:
//Composite order function
Function<Integer,Function<Integer,Integer>> fun= x->y->y+x;
System.out.println("Result 2: "+fun.apply(4).apply(2));//Prints 6
Let's write program to find nth Fibonacci number using FP
Function<Integer,Integer> fib = x-> x<=1?1:Sample.fib.apply(x-2)+Sample.fib.apply(x-1);
System.out.println("5th fib no."+fib.apply(5)); //Prints 8
Few other examples where we can use functional programming
//Function which increments a numerical string value by one
Function<String,String>incStr=s->String.valueOf (Integer.parseInt(s)
+1);
System.out.println(incStr.apply("13"));
Function<Function<Integer,Integer>,Integer> f=x->x.apply(4);
Function<Integer,Integer> innerFunc = x->x*20;
System.out.println("RESULT :"+f.apply(innerFunc));
This blog just explains very basic info about Functional Programming in Java for more info you click here
Happy Coding :)
Comments
Post a Comment