在Java中,您可以执行以下代码:
Function<Integer, Integer> times2 = e -> e * 2;
Function<Integer, Integer> squared = e -> e * e;
times2.andThen(squared).apply(4);
C++中与
andThen()
相对应的是什么,用于创建/组合新函数对象?谢谢。在Java中,您可以执行以下代码:
Function<Integer, Integer> times2 = e -> e * 2;
Function<Integer, Integer> squared = e -> e * e;
times2.andThen(squared).apply(4);
andThen()
相对应的是什么,用于创建/组合新函数对象?谢谢。compose
函数适配器。assert(compose(f, g)(xs...) == f(g(xs...)));
在你的情况下,你需要做
auto composed = compose(squared, times2);
auto result = composed(4);
请查看文档以获取详细信息https://www.boost.org/doc/libs/1_68_0/libs/hof/doc/html/include/boost/hof/compose.html
为什么不保持简单呢?
int times2thenSquared(int x) {
x = times2(x);
return squared(x);
}
(如果您愿意),也可以使用lambda完成它。