Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-12-15 09:53:03

0001 /*!
0002 @file
0003 Forward declares `boost::hana::filter`.
0004 
0005 Copyright Louis Dionne 2013-2022
0006 Distributed under the Boost Software License, Version 1.0.
0007 (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
0008  */
0009 
0010 #ifndef BOOST_HANA_FWD_FILTER_HPP
0011 #define BOOST_HANA_FWD_FILTER_HPP
0012 
0013 #include <boost/hana/config.hpp>
0014 #include <boost/hana/core/when.hpp>
0015 
0016 
0017 namespace boost { namespace hana {
0018     //! Filter a monadic structure using a custom predicate.
0019     //! @ingroup group-MonadPlus
0020     //!
0021     //! Given a monadic structure and a predicate, `filter` returns a new
0022     //! monadic structure containing only those elements that satisfy the
0023     //! predicate. This is a generalization of the usual `filter` function
0024     //! for sequences; it works for any MonadPlus. Intuitively, `filter` is
0025     //! somewhat equivalent to:
0026     //! @code
0027     //!     filter(xs, pred) == flatten(transform(xs, [](auto x) {
0028     //!         return pred(x) ? lift<Xs>(x) : empty<Xs>();
0029     //!     })
0030     //! @endcode
0031     //! In other words, we basically turn a monadic structure containing
0032     //! `[x1, ..., xn]` into a monadic structure containing
0033     //! @code
0034     //!     [
0035     //!         pred(x1) ? [x1] : [],
0036     //!         pred(x2) ? [x2] : [],
0037     //!         ...
0038     //!         pred(xn) ? [xn] : []
0039     //!     ]
0040     //! @endcode
0041     //! and we then `flatten` that.
0042     //!
0043     //!
0044     //! Signature
0045     //! ---------
0046     //! Given a `MonadPlus` `M` and an `IntegralConstant` `Bool` holding a
0047     //! value of type `bool`, the signature is
0048     //! @f$ \mathtt{filter} : M(T) \times (T \to \mathtt{Bool}) \to M(T) @f$.
0049     //!
0050     //! @param xs
0051     //! The monadic structure to filter.
0052     //!
0053     //! @param pred
0054     //! A function called as `pred(x)` for each element `x` in the monadic
0055     //! structure and returning whether that element should be __kept__ in
0056     //! the resulting structure. In the current version of the library, the
0057     //! predicate has to return an `IntegralConstant` holding a value
0058     //! convertible to a `bool`.
0059     //!
0060     //!
0061     //! Example
0062     //! -------
0063     //! @include example/filter.cpp
0064 #ifdef BOOST_HANA_DOXYGEN_INVOKED
0065     constexpr auto filter = [](auto&& xs, auto&& pred) {
0066         return tag-dispatched;
0067     };
0068 #else
0069     template <typename M, typename = void>
0070     struct filter_impl : filter_impl<M, when<true>> { };
0071 
0072     struct filter_t {
0073         template <typename Xs, typename Pred>
0074         constexpr auto operator()(Xs&& xs, Pred&& pred) const;
0075     };
0076 
0077     BOOST_HANA_INLINE_VARIABLE constexpr filter_t filter{};
0078 #endif
0079 }} // end namespace boost::hana
0080 
0081 #endif // !BOOST_HANA_FWD_FILTER_HPP