Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does lambda with 2 arrows mean in Java 8?

People also ask

What does the -> mean in Java?

Basically, the -> separates the parameters (left-side) from the implementation (right side). The general syntax for using lambda expressions is. (Parameters) -> { Body } where the -> separates parameters and lambda expression body.

What is the lambda expression in Java 8?

Lambda Expressions were added in Java 8. A lambda expression is a short block of code which takes in parameters and returns a value. Lambda expressions are similar to methods, but they do not need a name and they can be implemented right in the body of a method.

What do the arrows mean in Java?

To support lambdas, Java has introduced a new operator “->”, also known as lambda operator or arrow operator. This arrow operator is required because we need to syntactically separate the parameter from the body. LambdaBody can be an expression or a block.

Why do we add lambda in Java 8?

Introduction. Lambda expressions are a new and important feature included in Java SE 8. They provide a clear and concise way to represent one method interface using an expression. Lambda expressions also improve the Collection libraries making it easier to iterate through, filter, and extract data from a Collection .


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;
    }
};

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;
            }
        };
    }
};

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;
};

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>.