summaryrefslogtreecommitdiff
path: root/boost/hana/fwd/filter.hpp
diff options
context:
space:
mode:
Diffstat (limited to 'boost/hana/fwd/filter.hpp')
-rw-r--r--boost/hana/fwd/filter.hpp88
1 files changed, 88 insertions, 0 deletions
diff --git a/boost/hana/fwd/filter.hpp b/boost/hana/fwd/filter.hpp
new file mode 100644
index 0000000000..7152361ef7
--- /dev/null
+++ b/boost/hana/fwd/filter.hpp
@@ -0,0 +1,88 @@
+/*!
+@file
+Forward declares `boost::hana::filter`.
+
+@copyright Louis Dionne 2013-2016
+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
+ //!
+ //! Benchmarks
+ //! ----------
+ //! <div class="benchmark-chart"
+ //! style="min-width: 310px; height: 400px; margin: 0 auto"
+ //! data-dataset="benchmark.filter.compile.json">
+ //! </div>
+#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