在Java中,您可以执行以下代码:
Function<Integer, Integer> times2 = e -> e * 2;
Function<Integer, Integer> squared = e -> e * e;
times2.andThen(squared).apply(4);
C++ 相当于什么andThen()
铸造/复合新函子?谢谢。
如果您愿意使用 Boost,那么 Boost.HOF 就是您所需要的。 HOF(高阶函数)提供了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 https://www.boost.org/doc/libs/1_68_0/libs/hof/doc/html/include/boost/hof/compose.html
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)