以功能方式组合谓词 [英] Combining predicates in a functional way
本文介绍了以功能方式组合谓词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Boost Hana是否提供将谓词与逻辑运算符组合的方法?
Does Boost Hana provide a way to combine predicates with logical operators?
我指的是这样的东西
constexpr auto both = [](auto&& f, auto&& g){
return [&f,&g](auto&& x){ return f(x) && g(x); };
};
可以这样使用:
int main() {
std::vector<int> v{1,2,3,4,5,6,7,8,9,10};
auto less_than_7 = hana::reverse_partial(std::less_equal<int>{}, 7);
auto more_than_3 = hana::reverse_partial(std::greater_equal<int>{}, 3);
auto r = ranges::views::remove_if(v, both(less_than_7, more_than_3));
}
where, from Hana, I'd expect also a way to use it in an infix way similar to hana::on
, as in both(less_than_7, more_than_3) === less_than_7 ^both^ more_than_3
(even though and
would be nicer, but I've just found that it's a synonym for &&
).
或者它是否提供一种解除标准运算符&&
以便在函数函子上进行操作的方法?
Or maybe does it offer a way to lift the standard operator &&
to operate on function functors?
推荐答案
You could use demux
like:
auto both = hana::demux(std::logical_and<bool>{});
// Or for any number of predicates
auto all = hana::demux([](auto const&... x) noexcept(noexcept((static_cast<bool>(x), ...))) { return (true && ... && static_cast<bool>(x)); });
ranges::views::remove_if(v, both(less_than_7, more_than_3));
这篇关于以功能方式组合谓词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文