In Java, you can do the following code:
Function<Integer, Integer> times2 = e -> e * 2;
Function<Integer, Integer> squared = e -> e * e;
times2.andThen(squared).apply(4);
What's C++'s equivalent of andThen() to coin/composite new functors? Thanks.
If you are open to using Boost, then Boost.HOF is what you need. HOF (Higher order functions) provides the compose function adapter with the following semantics
assert(compose(f, g)(xs...) == f(g(xs...)));
In your case, you will do
auto composed = compose(squared, times2);
auto result = composed(4);
Have a look at the documentation for details https://www.boost.org/doc/libs/1_68_0/libs/hof/doc/html/include/boost/hof/compose.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With