Currying in Java 8

Currying in Java 8

dc7de2_1b30f5af83ad4a46920da3fb109dc377-jpg_srz_500_500_75_22_0-5_1-2_75_png_srz

In the new Java functional library, there are a set of interfaces for supporting Lambda expressions (the lambda expression is cast to appropriate interface).
Example of these are Function and BiFunction, where the later takes two arguments and returns nothing.

But what if we want to create a Lambda expression with more than two arguments?

You can always create your own functional interface, that can take the number of arguments that you want.


public interface TriFunction<T, U, V, R> {
  public R accept(T t, U u, V v);

}

To use the interface, it would be something like this.


TriFunction<Integer,Integer,Integer, Integer> triFunction = (x,y,z) -> (x+y)*z);

Integer result = triFunction.accept(1,2,3);

This would mean that we have to create a new interface for every possible number of arguments.

But there is another way.

Currying

Currying is a term within functional programming, where you take the arguments and break them down to a series of functions that takes a single argument. For example if we have:

f(x,y,z) = (x+y)/z

We could apply currying by applying only one argument and get a new function back that takes two arguments instead:

f(3,y,z) = g(y,z) = (3+y)/z

Apply currying gain, would give us:

f(3,4,z) = g(z) = (3+4)/z; 

So instead of creating the TriFunction interface above, we could use the Function interface.
Note, we need to use one of the interfaces that can return a functional interface.



Function<Integer, Function<Integer, Function<Integer, Integer>>> currying =  x -> y -> z -> (x+y)*z;

currying.apply(1).apply(2).apply(3);


To understand it better, we could look how it looks with Anonymous inner classes (pre Java 8).
Here we could clearly see that each method takes a single argument and returns a new Function.


Function<Integer, Function<Integer, Function<Integer, Integer>>> currying = new Function<Integer, Function<Integer, Function<Integer, Integer>>>() {
            @Override
            public Function<Integer, Function<Integer, Integer>> apply(Integer x) {
                return new Function<Integer, Function<Integer, Integer>>() {
                    @Override
                    public Function<Integer, Integer> apply(Integer y) {
                        return new Function<Integer, Integer>() {
                            @Override
                            public Integer apply(Integer z) {
                                return (x + y) * z;
                            }
                        };
                    }
                };
            }
        };

Well, this was a short post of how it is possible to use currying in Java 8, but it should be stressed that Java is not a functional language, and these is mostly syntactic sugar.

But it might be good to know that you can do it in Java, and there is not need to create new interfaces for each number of argument that you need.

Also, it’s a matter of preferences, which method you think is most readable and easy to understand (not only for you, but also the next person that is going to read your code). Always remember:

“Always code as if the person who ends up maintaining your code is a violent psychopath who knows where you live.” – Martin Golding

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.