One of the key identifiers of functional programming is Currying.
Since Java 8, the new features were added, allows us to do this operation.
What is currying? currying is a name assigned to ‘half initialized functions’.
A Curried function has some of the parameters fixed while other parameters will be passed by the users.
We use functional Interfaces In Java to achieve this operation.
You can watch my live coding youtube videos to see, how currying is done.
For example : If we want to create a function which always adds a given Int-value to an int-argumet. Then that can be defined like the below code.
private UnaryOperator<Integer> adderGenerator(final int adderValue){ return (intOperand) -> intOperand + adderValue; }
In the above function adderGenerator , we pass an int-value which will be get added to an int-argument.
So, If we want to create a function that always adds 2 to an int-argument, then that can be implemented like this.
final UnaryOperator<Integer> addTwoCurry = curry.adderGenerator(2);
In the above code, you can see that I am passing 2, which will get added to int-argument whenever addTwoCurry function gets called.
addTwoCurry.apply(3)
The above code will return 5 .
Similarly, we can curry by accepting a function as an argument.
private Consumer<String> displayComposer(final Consumer<String> displayer){ return (stringValue) -> displayer.accept(stringValue); }
The above code accepts a function and uses that function to generate a curried display function.
final Consumer<String> printToConsole = curry.displayComposer(System.out::println);
The displayComposer function will returned a curried display function, which is going to use System.out::println function to print any given value;
This concept can be applied in other scenarios.
Source Code :-
import java.util.function.UnaryOperator; import java.util.function.Consumer; public class Currying { private UnaryOperator<Integer> adderGenerator(final int adderValue){ return (intOperand) -> intOperand + adderValue; } private UnaryOperator<Integer> multiplierGenerator(final int multiplier){ return (intOperand) -> intOperand * multiplier; } private Consumer<String> displayComposer(final Consumer<String> displayer){ return (stringValue) -> displayer.accept(stringValue); } private void displaySimple(final String value){ System.out.println(value); } private void designerDisplay(final String value){ System.out.println("***************************"); System.out.println(value); System.out.println("***************************"); } public static void main(String ...args){ final Currying curry = new Currying(); final UnaryOperator<Integer> addTwoCurry = curry.adderGenerator(2); final UnaryOperator<Integer> multiplyBy_100 = curry.multiplierGenerator(100); final Consumer<String> printToConsole = curry.displayComposer(System.out::println); final Consumer<String> printToConsoleNoNewLine = curry.displayComposer(System.out::print); final Consumer<String> designerDisplay = curry.displayComposer(curry::designerDisplay); printToConsole.accept("Add Two to 2 = " + addTwoCurry.apply(2)); printToConsole.accept("Add Two to 3 = " + addTwoCurry.apply(3)); printToConsoleNoNewLine.accept("Add Two to 4 = " + addTwoCurry.apply(4)); printToConsole.accept("Add Two to 2 = " + multiplyBy_100.apply(2)); designerDisplay.accept("Add Two to 3 = " + multiplyBy_100.apply(3)); designerDisplay.accept("Add Two to 4 = " + multiplyBy_100.apply(4)); } }