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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
|
/*!
@file
Forward declares `boost::hana::monadic_fold_left`.
@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_MONADIC_FOLD_LEFT_HPP
#define BOOST_HANA_FWD_MONADIC_FOLD_LEFT_HPP
#include <boost/hana/config.hpp>
#include <boost/hana/core/when.hpp>
BOOST_HANA_NAMESPACE_BEGIN
//! Monadic left-fold of a structure with a binary operation and an
//! optional initial reduction state.
//! @ingroup group-Foldable
//!
//! @note
//! This assumes the reader to be accustomed to non-monadic left-folds as
//! explained by `hana::fold_left`, and to have read the [primer]
//! (@ref monadic-folds) on monadic folds.
//!
//! `monadic_fold_left<M>` is a left-associative monadic fold. Given a
//! `Foldable` with linearization `[x1, ..., xn]`, a function `f` and an
//! optional initial state, `monadic_fold_left<M>` applies `f` as follows:
//! @code
//! // with state
//! ((((f(state, x1) | f(-, x2)) | f(-, x3)) | ...) | f(-, xn))
//!
//! // without state
//! ((((f(x1, x2) | f(-, x3)) | f(-, x4)) | ...) | f(-, xn))
//! @endcode
//!
//! where `f(-, xk)` denotes the partial application of `f` to `xk`, and
//! `|` is just the operator version of the monadic `chain`.
//!
//! When the structure is empty, one of two things may happen. If an
//! initial state was provided, it is lifted to the given Monad and
//! returned as-is. Otherwise, if the no-state version of the function
//! was used, an error is triggered. When the stucture contains a single
//! element and the no-state version of the function was used, that
//! single element is lifted into the given Monad and returned as is.
//!
//!
//! Signature
//! ---------
//! Given a `Monad` `M`, a `Foldable` `F`, an initial state of tag `S`,
//! and a function @f$ f : S \times T \to M(S) @f$, the signatures of
//! `monadic_fold_left<M>` are
//! \f[
//! \mathtt{monadic\_fold\_left}_M :
//! F(T) \times S \times (S \times T \to M(S)) \to M(S)
//! \f]
//!
//! for the version with an initial state, and
//! \f[
//! \mathtt{monadic\_fold\_left}_M :
//! F(T) \times (T \times T \to M(T)) \to M(T)
//! \f]
//!
//! for the version without an initial state.
//!
//! @tparam M
//! The Monad representing the monadic context in which the fold happens.
//! The return type of `f` must be in that Monad.
//!
//! @param xs
//! The structure to fold.
//!
//! @param state
//! The initial value used for folding. If the structure is empty, this
//! value is lifted in to the `M` Monad and then returned as-is.
//!
//! @param f
//! A binary function called as `f(state, x)`, where `state` is the result
//! accumulated so far and `x` is an element in the structure. The
//! function must return its result inside the `M` Monad.
//!
//!
//! Example
//! -------
//! @include example/monadic_fold_left.cpp
#ifdef BOOST_HANA_DOXYGEN_INVOKED
template <typename M>
constexpr auto monadic_fold_left = [](auto&& xs[, auto&& state], auto&& f) -> decltype(auto) {
return tag-dispatched;
};
#else
template <typename T, typename = void>
struct monadic_fold_left_impl : monadic_fold_left_impl<T, when<true>> { };
template <typename M>
struct monadic_fold_left_t;
template <typename M>
constexpr monadic_fold_left_t<M> monadic_fold_left{};
#endif
BOOST_HANA_NAMESPACE_END
#endif // !BOOST_HANA_FWD_MONADIC_FOLD_LEFT_HPP
|