Maddy
Tech with Maddy

Tech with Maddy

What is function currying in Java?

What is function currying in Java?

Maddy's photo
Maddy

Published on Nov 7, 2021

2 min read

Subscribe to my newsletter and never miss my upcoming articles

Some days ago I was exploring the codebase at my current workplace, and I came across something like this:

x -> y -> (x.doSomething)

I was already familiar with lambda expressions, but this one was completely new to me.

I researched this and found out that Java supports function currying.

WHAT IS "FUNCTION CURRYING" IN JAVA?

Wikipedia says that:

In mathematics and computer science, currying is the technique of converting a function that takes multiple arguments into a sequence of functions that each takes a single argument.

In short, it's saying that, if we have a function that has many arguments, we can turn that function into a series of "mini-functions" that take only one argument, until we return all the parameters.

Let's look at an example:

package JavaCurrying;

public class UnderstandingCurrying {

    public int multiplyNumbers(int firstNumber, int secondNumber, int thirdNumber){
        return firstNumber * secondNumber * thirdNumber;
    }

}

Above, we have a method called multiplyNumbers that given three integers it returns its multiplication.

The same code snippet above can be written in the form of a lambda expression like this:

    public static
        Function<Integer,
                Function<Integer,
                    Function<Integer, Integer>>> curry() {
        return f -> (s -> (t -> multiplyNumbers(f, s, t)));
    }

I like to visualize function currying like a cascading waterfall!

waterfall.png

The code snippet above looks scary, but let's break it down so we can understand it. 😁

functionCurrying.jpg

The return type of curry() works like this:

  1. At line 12, we have a function that takes in an Integer.

  2. This function returns another function at line 13, which again returns an Integer.

  3. Then, this function returns another function at line 14. The function at line 14 takes in two parameters: the first one is the type of the parameter that it accepts (an Integer), the second is the type of the result that we're going to get when we perform the whole operation (an Integer).

In the main method, we would have something like this:

    public static void main(String[] args) {
        //using ordinary method
        Integer result = multiplyNumbers(1, 2, 3);
        System.out.println("Result using ordinary method " + result);

        //using curried function
        Integer curriedResult = curry()
                .apply(1)
                .apply(2)
                .apply(3);
        System.out.println("Result using curried function " + curriedResult);
    }

I hope this article makes sense. If you're struggling to grasp the return type of curry(), try to break it down into multiple lines. That should help you understand it better.

Until next time! 😊

ADDITIONAL REFERENCES:

 
Share this