Given some Java 8 method functions:
class Foo { Bar getBar() {} }
class Bar { Baz getBaz() {} }
A composition of the two accessors looks like:
Function<Foo, Bar> getBarFromFoo = Foo::getBar;
Function<Bar, Baz> getBazFromBar = Bar::getBaz;
Function<Foo, Baz> getBazFromFoo = getBarFromFoo.andThen(getBazFromBar);
Is there a more concise way? This seems to work
((Function<Foo, Bar>) Foo::getBar).andThen(Bar::getBaz)
But it's rather ugly. The outer parens make sense for precedence reasons, but why is the cast necessary?
(Foo::getBar::getBaz would be nice, but alas...)
foo -> foo.getBar()::getBaz? Seems like you're overcomplicating it. Is there something I'm missing?foo -> foo.getBar().getBaz()? otherwise, it makes no sensea->b,b->c) composing them with simple operations likeFunction'scompose,andThenat runtime rather than building up all possible cases at compile time.foo -> bar -> bar::getBazfoo -> bar -> bar::getBazto achieve a map series (here, to get aBazas output). It could return sth likeFunction<Foo, Function<Bar, Function<Bar, Baz>>>which doesn't make sense