I was comparing the use of compose()
default method with its equivalent lambda expression and couldn't find any reason why prefering the former.
Maybe the reason is related to code expressiveness or functional style programming, as pointed here
However, comparing the two approaches to solve the question How to do function composition? I see only disadvantages in using compose()
instead of lambda expressions.
1st approach with compose()
according to the accepted answer:
Function<Person, Address> personToAddress = Person::getAddress;
Function<Address, String> addressToCountry = Address::getCountry;
Function<Person, String> toCountry = addressToCountry.compose(personToAddress);
2nd approach with a lambda expression:
Function<Person, String> toCountry = p -> p.getAddress().getCountry();
Why do I prefer the second one? First, it is less verbose . Second, it does not require the explicit auxiliary variables personToAddress
and addressToCountry
.
So, is there any reason for using compose()
instead of using a lambda explicitly?