AdvancedExpressionFolding
AdvancedExpressionFolding copied to clipboard
map / orElse foldings
Java 8 code contains a lot of stuff like
optional.map(Obj::x).map(it -> it.y()).orElse(z())
it would be nice to have on the screen just:
optional?.x()?.y() ?: z()
Similarly for streams:
stream.map(Obj::x).map(it -> it.y())
can be:
stream*.x()*.y()
Here we can split into two things:
- Fold
Optionalto null safe operators. For example on my working project it's very popular to use Optional as Elvis operator:
BigDecimal totalAmount = Optional .ofNullable(amount).orElse(BigDecimal.ZERO);
- Replace
Stream.map()with spread operator. But here is not so straightforward: there is othermap*functions, it will look not consistent to others.
@iirekm @stokito I have incorporated the Optional feature, and I plan to implement the spread operator in upcoming implementations. For further details, please refer to my branch.
https://github.com/AntoniRokitnicki/AdvancedExpressionFolding/pull/10