Been presented several situations where it'd have been cool to have Optional keeping references to previously mapped objects.
// Normally the following is a good candidate for Optional fluent API (imagine also all the null checks that could be necessary)
A a = new A();
B b = calculateB(a);
C c = calculateC(b)
D d = calculateD(c)
E e = calculateE(d)
// As
E e = Optional.ofNullable(a)
.map(this::calculateB)
...
// But the following can't be expressed with Optional API
A a = new A();
B b = calculateB(a);
C c = calculateC(a, b)
D d = calculateD(a, c)
E e = calculateE(a, d)
// Something like the following sounds tempting
PowerOptional.ofNullable(a)
.mapAndKeep(this::calculateB) // mapAndKeep would carry the input as well to next call.
.map((a, b) -> calculateC(a, b))
.map((a, c) -> calculateD(a, c))
.map((a, d) -> calculateE(a, d))
.orElse(null);
This could improve readability of rather cumbersome methods. Is there a library that addresses this? Ideas (also on naming)?