3 Forward declares `boost::hana::scan_right`.
5 @copyright Louis Dionne 2013-2016
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_SCAN_RIGHT_HPP
11 #define BOOST_HANA_FWD_SCAN_RIGHT_HPP
13 #include <boost/hana/config.hpp>
14 #include <boost/hana/core/when.hpp>
17 BOOST_HANA_NAMESPACE_BEGIN
18 //! Fold a Sequence to the right and return a list containing the
19 //! successive reduction states.
20 //! @ingroup group-Sequence
22 //! Like `fold_right`, `scan_right` reduces a sequence to a single value
23 //! using a binary operation. However, unlike `fold_right`, it builds up
24 //! a sequence of the intermediary results computed along the way and
25 //! returns that instead of only the final reduction state. Like
26 //! `fold_right`, `scan_right` can be used with or without an initial
29 //! When the sequence is empty, two things may arise. If an initial state
30 //! was provided, a singleton list containing that state is returned.
31 //! Otherwise, if no initial state was provided, an empty list is
32 //! returned. In particular, unlike for `fold_right`, using `scan_right`
33 //! on an empty sequence without an initial state is not an error.
35 //! More specifically, `scan_right([x1, ..., xn], state, f)` is a sequence
36 //! whose `i`th element is equivalent to `fold_right([x1, ..., xi], state, f)`.
37 //! The no-state variant is handled in an analogous way. For illustration,
38 //! consider this right fold on a short sequence:
40 //! fold_right([x1, x2, x3], state, f) == f(x1, f(x2, f(x3, state)))
43 //! The analogous sequence generated with `scan_right` will be
45 //! scan_right([x1, x2, x3], state, f) == [
46 //! f(x1, f(x2, f(x3, state))),
47 //! f(x2, f(x3, state)),
53 //! Similarly, consider this right fold (without an initial state) on
56 //! fold_right([x1, x2, x3, x4], f) == f(x1, f(x2, f(x3, x4)))
59 //! The analogous sequence generated with `scan_right` will be
61 //! scan_right([x1, x2, x3, x4], f) == [
62 //! f(x1, f(x2, f(x3, x4))),
70 //! The sequence to scan from the right.
73 //! The (optional) initial reduction state.
76 //! A binary function called as `f(x, state)`, where `state` is the
77 //! result accumulated so far and `x` is an element in the sequence.
78 //! When no initial state is provided, `f` is called as `f(x1, x2)`,
79 //! where `x1` and `x2` are elements of the sequence.
84 //! @include example/scan_right.cpp
85 #ifdef BOOST_HANA_DOXYGEN_INVOKED
86 constexpr auto scan_right = [](auto&& xs[, auto&& state], auto const& f) {
87 return tag-dispatched;
90 template <typename S, typename = void>
91 struct scan_right_impl : scan_right_impl<S, when<true>> { };
94 template <typename Xs, typename State, typename F>
95 constexpr auto operator()(Xs&& xs, State&& state, F const& f) const;
97 template <typename Xs, typename F>
98 constexpr auto operator()(Xs&& xs, F const& f) const;
101 constexpr scan_right_t scan_right{};
103 BOOST_HANA_NAMESPACE_END
105 #endif // !BOOST_HANA_FWD_SCAN_RIGHT_HPP