Introduction to Currying Function in Java
In Java, currying function is the mechanism of breaking down a multi-argument function to form the multiple functions with single arguments such that the output remains same. In other words, when we don't want to pass all the arguments to the function, we use currying. So, currying decomposes higher order functions into cascaded functions with one argument that return a function, except for the last cascade that returns the result of computation.
Hence, it simplifies the function call. The other advantage is that the currying function allows us to create more specialized function so as to allow code re-usability.
Syntax
The syntax of apply function is as shown below. The apply function applies to the given argument t and returns the result.
apply(T t)
Examples demonstrating currying in Java
Example 1 : Currying function to multiply two numbers
In this example, we are designing the curried function with two integer parameters to compute multiplication of two numbers. Here, we will use apply method to pass two parameters 10 and 20 to the curried function.
// Program to demonstrate the currying in Java
import java.util.function.Function;
// Main class
public class Main {
public static void main(String args[])
{
// Curried Function for Multiplying u & v
Function<Integer, Function<Integer, Integer> >
c = u -> v -> u * v;
// Calling Curried Function for Multiplying u & v
System.out.println("Product of 10 and 20 is "+ c.apply(10).apply(20));
}
}
Output
Product of 10 and 20 is 200
Example 2 : Currying function to concatenate the strings
In this example, we are designing the curried function with two string parameters to compute concatenation of two Strings. We will use apply method to concatenate the strings using the curried function.
// Program to demonstrate the currying in Java
import java.util.function.Function;
// Main class
public class Main {
public static void main(String args[])
{
// Curried Function for concatenating the strings
Function<String, Function<String, String> >
c = u -> v -> u + v;
// Calling Curried Function for concatenating the strings
System.out.println("Concate two string : "+ c.apply("Hello").apply("World"));
}
}
Output
Concate two string : HelloWorld
Example 3 : Currying function to evaluate the expression
In this example, we are designing the curried function with three integer parameters to evaluate the expression. We will use apply method to pass three parameters using the apply method and thereby evaluating the expression 2x+3y+5z to get its result.
// Program to demonstrate the currying in Java
import java.util.function.Function;
public class Main {
public static void main(String args[])
{
// Curried Function for Multiplying u & v
Function<Integer, Function<Integer, Function<Integer, Integer> > >
c = x -> y -> z-> 2*x + 3*y + 5*z;
// Calling Curried Function for Multiplying u & v
System.out.println("Evaluating 2x+3y+5z with x=10, y=20 and z=5 : "+ c.apply(10).apply(20).apply(5));
}
}
Output
Evaluating 2x+3y+5z with x=10, y=20 and z=5 : 105
Example 4 : Currying function using the values from the function call
In this example, we will call a function fun1, fun2 and fun3 to get three values to compute the value of expression. Thereafter, we will evaluate the expression 2x+3y+5z to get its result.
// Program to demonstrate the currying in Java
import java.util.function.Function;
public class Main {
public static int fun1()
{
return 10;
}
public static int fun2()
{
return 20;
}
public static int fun3()
{
return 5;
}
public static void main(String args[])
{
// Curried Function for Multiplying u & v
Function<Integer, Function<Integer, Function<Integer, Integer> > >
c = x -> y -> z-> 2*x + 3*y + 5*z;
// Calling Curried Function for Multiplying u & v
System.out.println("Evaluating 2x+3y+5z with x=10, y=20 and z=5 : "+ c.apply(fun1()).apply(fun2()).apply(fun3()));
}
}
Output
Evaluating 2x+3y+5z with x=10, y=20 and z=5 : 105
Example 5 : Currying function computing simple interest using Class
In this example, we are having a class SI that has a class variable rate which sets the rate depending on the type of account. Thereafter, we have a function getrate()
that returns the value of class variable rate.
In Main class we are scanning the principal amount and duration from the user. Thereafter, we will compute the simple interest using curried function and obtaining value from the getrate()
method of the SI class. Hence, it shows the simple interest computed taking principal, rate and duration.
// Program to demonstrate the currying in Java
import java.util.function.Function;
import java.util.Scanner;
class SI{
double rate;
SI(String t)
{
if(t=="Savings")
this.rate=7.5f;
else
this.rate=3.5f;
}
public double getrate()
{
return this.rate;
}
}
public class Main {
public static void main(String args[])
{
// Creating object of SI class to get the rate of interest
// Here we will get the rate for savings account
SI o=new SI("Savings");
// Curried Function for computing simple interest
Function<Double, Function<Double, Function<Double, Double> > >
c = x -> y -> z-> (x*y*z)/100.0;
// Getting principal and number of years from the user
Scanner sc=new Scanner(System.in);
System.out.println("Enter your principal amount ");
double p=sc.nextFloat();
System.out.println("Enter the duration of deposit in Years");
double n=sc.nextFloat();
// Calling Curried Function for computing simple interest
System.out.println("Simple Interest on principal "+p+" with rate "+o.getrate()+" for "+n+" years will be "+ c.apply(p).apply(o.getrate()).apply(n));
}
}
Output
Enter your principal amount
1000
Enter the duration of deposit in Years
2.5
Simple Interest on principal 1000.0 with rate 7.5 for 2.5 years will be 187.5
Example 6 : Currying function computing student score using class
In this example, We will take a class Student that sets the sport marks of student based on the total marks of the student. Thereafter it computes the final score using currying function in a Main class. The equation used to compute the score is score=total+sport*25
.
// Program to demonstrate the currying in Java
import java.util.function.Function;
import java.util.Scanner;
class Student{
int sport;
Student(int total)
{
if(total >210)
this.sport=5;
else if(total > 180)
this.sport=3;
else
this.sport=0;
}
public int getsport()
{
return this.sport;
}
}
public class Main {
public static void main(String args[])
{
// Getting marks of 3 subjects for a student
Scanner sc=new Scanner(System.in);
System.out.println("Enter marks of three subjects ");
int a=sc.nextInt();
int b=sc.nextInt();
int c=sc.nextInt();
int t=a+b+c;
// Creating object of Student class to get the sport marks on the basis of total marks
// Here we will get the rate for savings account
Student o=new Student(t);
// Curried Function for computing score
Function<Integer, Function<Integer, Integer> >
s = x -> y -> x + y * 25;
// Calling Curried Function for computing score
System.out.println("Total of 3 subjects will be "+t+". Sports marks will be "+o.getsport()+". So final result will be "+ s.apply(t).apply(o.getsport()));
}
}
Output
Enter marks of three subjects
60
70
65
Total of 3 subjects will be 195. Sports marks will be 3. So final result will be 270
Summary
The knowledge of currying functions in Java is very useful while working on real time applications. In this tutorial, we covered the different scenario to use function currying in java with an example. As per the requirement of an application, we can choose an appropriate approach for currying. We learned in detail about currying with an example.
All in all, this tutorial, covers everything that you need to know in order to have a clear view on currying function in Java.
References