1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
|
/*!
@file
Forward declares `boost::hana::filter`.
@copyright Louis Dionne 2013-2017
Distributed under the Boost Software License, Version 1.0.
(See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
*/
#ifndef BOOST_HANA_FWD_FILTER_HPP
#define BOOST_HANA_FWD_FILTER_HPP
#include <boost/hana/config.hpp>
#include <boost/hana/core/when.hpp>
BOOST_HANA_NAMESPACE_BEGIN
//! Filter a monadic structure using a custom predicate.
//! @ingroup group-MonadPlus
//!
//! Given a monadic structure and a predicate, `filter` returns a new
//! monadic structure containing only those elements that satisfy the
//! predicate. This is a generalization of the usual `filter` function
//! for sequences; it works for any MonadPlus. Intuitively, `filter` is
//! somewhat equivalent to:
//! @code
//! filter(xs, pred) == flatten(transform(xs, [](auto x) {
//! return pred(x) ? lift<Xs>(x) : empty<Xs>();
//! })
//! @endcode
//! In other words, we basically turn a monadic structure containing
//! `[x1, ..., xn]` into a monadic structure containing
//! @code
//! [
//! pred(x1) ? [x1] : [],
//! pred(x2) ? [x2] : [],
//! ...
//! pred(xn) ? [xn] : []
//! ]
//! @endcode
//! and we then `flatten` that.
//!
//!
//! Signature
//! ---------
//! Given a `MonadPlus` `M` and an `IntegralConstant` `Bool` holding a
//! value of type `bool`, the signature is
//! @f$ \mathtt{filter} : M(T) \times (T \to \mathtt{Bool}) \to M(T) @f$.
//!
//! @param xs
//! The monadic structure to filter.
//!
//! @param pred
//! A function called as `pred(x)` for each element `x` in the monadic
//! structure and returning whether that element should be __kept__ in
//! the resulting structure. In the current version of the library, the
//! predicate has to return an `IntegralConstant` holding a value
//! convertible to a `bool`.
//!
//!
//! Example
//! -------
//! @include example/filter.cpp
#ifdef BOOST_HANA_DOXYGEN_INVOKED
constexpr auto filter = [](auto&& xs, auto&& pred) {
return tag-dispatched;
};
#else
template <typename M, typename = void>
struct filter_impl : filter_impl<M, when<true>> { };
struct filter_t {
template <typename Xs, typename Pred>
constexpr auto operator()(Xs&& xs, Pred&& pred) const;
};
constexpr filter_t filter{};
#endif
BOOST_HANA_NAMESPACE_END
#endif // !BOOST_HANA_FWD_FILTER_HPP
|