121

I have read several Java 8 tutorials before.

Right now I encountered following topic: Does java support Currying?

Here, I see following code:

IntFunction<IntUnaryOperator> curriedAdd = a -> b -> a + b;
System.out.println(curriedAdd.apply(1).applyAsInt(12));

I understand that this example sum 2 elements but I cannot understand the construction:

a -> b -> a + b;

According to the left part of expression, this row should implement following function:

R apply(int value); 

Before this, I only met lambdas only with one arrow.

Community
  • 1
  • 1
gstackoverflow
  • 36,709
  • 117
  • 359
  • 710

6 Answers6

121

If you express this as non-shorthand lambda syntax or pre-lambda Java anonymous class syntax it is clearer what is happening...

The original question. Why are two arrows? Simple, there are two functions being defined... The first function is a function-defining-function, the second is the result of that function, which also happens to be function. Each requires an -> operator to define it.

Non-shorthand

IntFunction<IntUnaryOperator> curriedAdd = (a) -> {
    return (b) -> {
        return a + b;
    };
};

Pre-Lambda before Java 8

IntFunction<IntUnaryOperator> curriedAdd = new IntFunction<IntUnaryOperator>() {
    @Override
    public IntUnaryOperator apply(final int value) {
        IntUnaryOperator op = new IntUnaryOperator() {
            @Override
            public int applyAsInt(int operand) {
                return operand + value;
            }
        };
        return op;
    }
};
Adam
  • 35,919
  • 9
  • 100
  • 137
49

An IntFunction<R> is a function int -> R. An IntUnaryOperator is a function int -> int.

Thus an IntFunction<IntUnaryOperator> is a function that takes an int as parameter and return a function that takes an int as parameter and return an int.

a -> b -> a + b;
^    |         |
|     ---------
|         ^
|         |
|         The IntUnaryOperator (that takes an int, b) and return an int (the sum of a and b)
|
The parameter you give to the IntFunction

Maybe it is more clear if you use anonymous classes to "decompose" the lambda:

IntFunction<IntUnaryOperator> add = new IntFunction<IntUnaryOperator>() {
    @Override
    public IntUnaryOperator apply(int a) {
        return new IntUnaryOperator() {
            @Override
            public int applyAsInt(int b) {
                return a + b;
            }
        };
    }
};
Alexis C.
  • 91,686
  • 21
  • 171
  • 177
30

Adding parentheses may make this more clear:

IntFunction<IntUnaryOperator> curriedAdd = a -> (b -> (a + b));

Or probably intermediate variable may help:

IntFunction<IntUnaryOperator> curriedAdd = a -> {
    IntUnaryOperator op = b -> a + b;
    return op;
};
Tagir Valeev
  • 97,161
  • 19
  • 222
  • 334
24

Let's rewrite that lambda expression with parentheses to make it more clear:

IntFunction<IntUnaryOperator> curriedAdd = a -> (b -> (a + b));

So we are declaring a function taking an int which returns a Function. More specifically, the function returned takes an int and returns an int (the sum of the two elements): this can be represented as an IntUnaryOperator.

Therefore, curriedAdd is a function taking an int and returning an IntUnaryOperator, so it can be represented as IntFunction<IntUnaryOperator>.

Tunaki
  • 132,869
  • 46
  • 340
  • 423
10

It's two lambda expressions.

IntFunction<IntUnaryOperator> curriedAdd = 
  a -> { //this is for the fixed value
    return b -> { //this is for the add operation
      return a + b;
    };
  }

IntUnaryOperator addTwo = curriedAdd.apply(2);
System.out.println(addTwo.applyAsInt(12)); //prints 14
a better oliver
  • 26,330
  • 2
  • 58
  • 66
8

If you look at IntFunction it might become clearer: IntFunction<R> is a FunctionalInterface. It represents a function that takes an int and returns a value of type R.

In this case, the return type R is also a FunctionalInterface, namely an IntUnaryOperator. So the first (outer) function itself returns a function.

In this case: When applied to an int, curriedAdd is supposed to return a function that again takes an int (and returns again int, because that's what IntUnaryOperator does).

In functional programming it is common to write the type of a function as param -> return_value and you see exactly that here. So the type of curriedAdd is int -> int -> int (or int -> (int -> int) if you like that better).

Java 8's lambda syntax goes along with this. To define such a function, you write

a -> b -> a + b

which is very much similar to actual lambda calculus:

λa λb a + b

λb a + b is a function that takes a single parameter b and returns a value (the sum). λa λb a + b is a function that accepts a single parameter a and returns another function of a single parameter. λa λb a + b returns λb a + b with a set to the parameter value.

dhke
  • 15,008
  • 2
  • 39
  • 56