- Sometimes, all inputs to a function are not available at the time of function application.
- Thus, we can apply only the available inputs, and then return a function which can be applied to the remaining inputs, whenever those remaining inputs are available.
- A curried function is one which can accept partial inputs.
- Currying is the process of making an un-curried function curried.
@Slf4j
public class Currying {
static BiFunction<String, String, String> uncurried = (a, b) -> "Hello " + a + b;
static Function<String, Function<String, String>> curried = a -> (b -> "Hello " + a + b);
public static void main(String[] args) {
log.info("uncurried function output: " + uncurried.apply("Tom", "Cruise"));
Function<String, String> partiallyAppliedFunction = curried.apply("Tom");
log.info("finally, curried function output: " + partiallyAppliedFunction.apply("Cruise"));
}
}
Comments
Post a comment