Saturday, 19 October 2019

Currying Functions in Java

Currying Functions in Java

single-valued argument multi-functions.
Function Currying is a concept of breaking a function with many arguments into many functions with single argument in such a way, that the output is same.


Example 1: Adding 2 numbers using Function Currying

// Java Program to demonstrate Function Currying 

import java.util.function.Function; 

public class GFG { 
public static void main(String args[]) 

// Using Java 8 Functions 
// to create lambda expressions for functions 
// and with this, applying Function Currying 

// Curried Function for Adding u & v 
Function<Integer, 
Function<Integer, Integer> > 
curryAdder = u -> v -> u + v; 

// Calling the curried functions 

// Calling Curried Function for Adding u & v 
System.out.println("Add 2, 3 :"
+ curryAdder 
.apply(2) 
.apply(3)); 



Output::
Add 2, 3 :5


No comments:

Post a Comment

Note: only a member of this blog may post a comment.