Why can't we compose Function and Consumer just like we compose functions?
Function<Integer, String> a = Object::toString;
Consumer<String> b = x -> System.out.println(x);
Consumer<Integer> composed = a.andThen(b);
This seems like an obvious improvisation to the existing Function interface. Is there any reason why this capability was avoided in Java 8?
Also, is it a common practice to use an extended implementation of Function as follows?
interface FunctionImproved<T, R> extends Function<T, R> {
default Consumer<T> andThen(Consumer<R> consumer) {
return x -> consumer.accept(apply(x));
}
}