You have partially applied add
. This is related to currying.
In some languages that support partial application, functions are curried by default. you might be able write code like:
increment = add(1)
println(increment(2))
# => 3
A curried function allows you to partially apply that function directly. Java doesn't support that kind of thing without extra machinery.
EDIT:
In Java 8, with lambdas and java.util.function
, you can define a curry function.
import java.util.function.Function;
public class Example {
public static <T, U, R> Function<T, Function<U, R>> curry(BiFunction<T, U, R> f) {
return t -> u -> f.apply(t, u);
}
public static int add(int x, int y) {
return x + y;
}
public static void main(String[] args) {
Function<Integer, Function<Integer, Integer>> curriedAdd = curry(Example::add);
// or
// BiFunction<Integer, Integer, Integer> add = (x, y) -> x + y;
// curriedAdd = curry(add);
Function<Integer, Integer> increment = curriedAdd.apply(1);
System.out.println(increment.apply(4));
}
}
EDIT #2:
I was wrong! I've corrected/modified my answer. As sepp2k pointed out this is only partial function application. The two concepts are related and often confused. In my defense there's a section on the currying Wikipedia page about the mixup.