3 Forward declares `boost::hana::fold_left`.
5 @copyright Louis Dionne 2013-2017
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)
10 #ifndef BOOST_HANA_FWD_FOLD_LEFT_HPP
11 #define BOOST_HANA_FWD_FOLD_LEFT_HPP
13 #include <boost/hana/config.hpp>
14 #include <boost/hana/core/when.hpp>
17 BOOST_HANA_NAMESPACE_BEGIN
18 //! Left-fold of a structure using a binary operation and an optional
19 //! initial reduction state.
20 //! @ingroup group-Foldable
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
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
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.
39 //! Given a `Foldable` `F` and an optional initial state of tag `S`,
40 //! the signatures for `fold_left` are
42 //! \mathtt{fold\_left} : F(T) \times S \times (S \times T \to S) \to S
45 //! for the variant with an initial state, and
47 //! \mathtt{fold\_left} : F(T) \times (T \times T \to T) \to T
50 //! for the variant without an initial state.
53 //! The structure to fold.
56 //! The initial value used for folding.
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.
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;
73 template <typename T, typename = void>
74 struct fold_left_impl : fold_left_impl<T, when<true>> { };
77 template <typename Xs, typename State, typename F>
78 constexpr decltype(auto) operator()(Xs&& xs, State&& state, F&& f) const;
80 template <typename Xs, typename F>
81 constexpr decltype(auto) operator()(Xs&& xs, F&& f) const;
84 constexpr fold_left_t fold_left{};
86 BOOST_HANA_NAMESPACE_END
88 #endif // !BOOST_HANA_FWD_FOLD_LEFT_HPP