• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1.. Algorithms/Iteration Algorithms//fold
2
3fold
4====
5
6Synopsis
7--------
8
9.. parsed-literal::
10
11    template<
12          typename Sequence
13        , typename State
14        , typename ForwardOp
15        >
16    struct fold
17    {
18        typedef |unspecified| type;
19    };
20
21
22
23Description
24-----------
25
26Returns the result of the successive application of binary ``ForwardOp`` to the
27result of the previous ``ForwardOp`` invocation (``State`` if it's the first call)
28and every element of the sequence in the range |begin/end<Sequence>| in order.
29
30
31Header
32------
33
34.. parsed-literal::
35
36    #include <boost/mpl/fold.hpp>
37
38
39Parameters
40----------
41
42+---------------+-------------------------------+---------------------------------------------------+
43| Parameter     | Requirement                   | Description                                       |
44+===============+===============================+===================================================+
45| ``Sequence``  | |Forward Sequence|            | A sequence to iterate.                            |
46+---------------+-------------------------------+---------------------------------------------------+
47| ``State``     | Any type                      | The initial state for the first ``ForwardOp``     |
48|               |                               | application.                                      |
49+---------------+-------------------------------+---------------------------------------------------+
50| ``ForwardOp`` | Binary |Lambda Expression|    | The operation to be executed on forward           |
51|               |                               | traversal.                                        |
52+---------------+-------------------------------+---------------------------------------------------+
53
54
55Expression semantics
56--------------------
57
58For any |Forward Sequence| ``s``, binary |Lambda Expression| ``op``, and arbitrary type ``state``:
59
60
61.. parsed-literal::
62
63    typedef fold<s,state,op>::type t;
64
65:Return type:
66    A type.
67
68:Semantics:
69    Equivalent to
70
71    .. parsed-literal::
72
73        typedef iter_fold<
74              s
75            , state
76            , apply_wrap2< lambda<op>::type, _1, deref<_2> >
77            >::type t;
78
79
80Complexity
81----------
82
83Linear. Exactly ``size<s>::value`` applications of ``op``.
84
85
86Example
87-------
88
89.. parsed-literal::
90
91    typedef vector<long,float,short,double,float,long,long double> types;
92    typedef fold<
93          types
94        , int_<0>
95        , if_< is_float<_2>,next<_1>,_1 >
96        >::type number_of_floats;
97
98    BOOST_MPL_ASSERT_RELATION( number_of_floats::value, ==, 4 );
99
100
101See also
102--------
103
104|Algorithms|, |accumulate|, |reverse_fold|, |iter_fold|, |reverse_iter_fold|, |copy|, |copy_if|
105
106
107.. copyright:: Copyright �  2001-2009 Aleksey Gurtovoy and David Abrahams
108   Distributed under the Boost Software License, Version 1.0. (See accompanying
109   file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
110