fold_left.hpp 2.9 KB

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