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.
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.
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.
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.
IntFunction<IntUnaryOperator> curriedAdd = (a) -> {
return (b) -> {
return a + b;
};
};
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>
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With