| 1 | /*! |
| 2 | @file |
| 3 | Forward declares `boost::hana::fold_left`. |
| 4 | |
| 5 | Copyright Louis Dionne 2013-2022 |
| 6 | Distributed under the Boost Software License, Version 1.0. |
| 7 | (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt) |
| 8 | */ |
| 9 | |
| 10 | #ifndef BOOST_HANA_FWD_FOLD_LEFT_HPP |
| 11 | #define BOOST_HANA_FWD_FOLD_LEFT_HPP |
| 12 | |
| 13 | #include <boost/hana/config.hpp> |
| 14 | #include <boost/hana/core/when.hpp> |
| 15 | |
| 16 | |
| 17 | namespace boost { namespace hana { |
| 18 | //! Left-fold of a structure using a binary operation and an optional |
| 19 | //! initial reduction state. |
| 20 | //! @ingroup group-Foldable |
| 21 | //! |
| 22 | //! `fold_left` is a left-associative fold using a binary operation. |
| 23 | //! Given a structure containing `x1, ..., xn`, a function `f` and |
| 24 | //! an optional initial state, `fold_left` applies `f` as follows |
| 25 | //! @code |
| 26 | //! f(... f(f(f(x1, x2), x3), x4) ..., xn) // without state |
| 27 | //! f(... f(f(f(f(state, x1), x2), x3), x4) ..., xn) // with state |
| 28 | //! @endcode |
| 29 | //! |
| 30 | //! When the structure is empty, two things may arise. If an initial |
| 31 | //! state was provided, it is returned as-is. Otherwise, if the no-state |
| 32 | //! version of the function was used, an error is triggered. When the |
| 33 | //! stucture contains a single element and the no-state version of the |
| 34 | //! function was used, that single element is returned as is. |
| 35 | //! |
| 36 | //! |
| 37 | //! Signature |
| 38 | //! --------- |
| 39 | //! Given a `Foldable` `F` and an optional initial state of tag `S`, |
| 40 | //! the signatures for `fold_left` are |
| 41 | //! \f[ |
| 42 | //! \mathtt{fold\_left} : F(T) \times S \times (S \times T \to S) \to S |
| 43 | //! \f] |
| 44 | //! |
| 45 | //! for the variant with an initial state, and |
| 46 | //! \f[ |
| 47 | //! \mathtt{fold\_left} : F(T) \times (T \times T \to T) \to T |
| 48 | //! \f] |
| 49 | //! |
| 50 | //! for the variant without an initial state. |
| 51 | //! |
| 52 | //! @param xs |
| 53 | //! The structure to fold. |
| 54 | //! |
| 55 | //! @param state |
| 56 | //! The initial value used for folding. |
| 57 | //! |
| 58 | //! @param f |
| 59 | //! A binary function called as `f(state, x)`, where `state` is the |
| 60 | //! result accumulated so far and `x` is an element in the structure. |
| 61 | //! For left folds without an initial state, the function is called as |
| 62 | //! `f(x1, x2)`, where `x1` and `x2` are elements of the structure. |
| 63 | //! |
| 64 | //! |
| 65 | //! Example |
| 66 | //! ------- |
| 67 | //! @include example/fold_left.cpp |
| 68 | #ifdef BOOST_HANA_DOXYGEN_INVOKED |
| 69 | constexpr auto fold_left = [](auto&& xs[, auto&& state], auto&& f) -> decltype(auto) { |
| 70 | return tag-dispatched; |
| 71 | }; |
| 72 | #else |
| 73 | template <typename T, typename = void> |
| 74 | struct fold_left_impl : fold_left_impl<T, when<true>> { }; |
| 75 | |
| 76 | struct fold_left_t { |
| 77 | template <typename Xs, typename State, typename F> |
| 78 | constexpr decltype(auto) operator()(Xs&& xs, State&& state, F&& f) const; |
| 79 | |
| 80 | template <typename Xs, typename F> |
| 81 | constexpr decltype(auto) operator()(Xs&& xs, F&& f) const; |
| 82 | }; |
| 83 | |
| 84 | BOOST_HANA_INLINE_VARIABLE constexpr fold_left_t fold_left{}; |
| 85 | #endif |
| 86 | }} // end namespace boost::hana |
| 87 | |
| 88 | #endif // !BOOST_HANA_FWD_FOLD_LEFT_HPP |
| 89 | |