
AMDG Mitch Besser wrote:
I would like to take the following Sequence of Tuples:
((A, 1)) ((B, 1)) ((C, 1)) ((D, 2)) ((E, 4))
and create a summation as follows:
((A, 0)) ((B, 1)) ((C, 2)) ((D, 3)) ((E, 5))
#include <boost/preprocessor/seq/fold_left.hpp> #include <boost/preprocessor/seq/push_back.hpp> #include <boost/preprocessor/seq/seq.hpp> #include <boost/preprocessor/tuple/elem.hpp> #include <boost/preprocessor/arithmetic/add.hpp> #define SHIFTED_SUM_OP(s, state, seq) \ (BOOST_PP_SEQ_PUSH_BACK( \ BOOST_PP_TUPLE_ELEM(2, 0, state), \ (BOOST_PP_TUPLE_ELEM(2, 0, seq), \ BOOST_PP_TUPLE_ELEM(2, 1, state))),\ BOOST_PP_ADD( \ BOOST_PP_TUPLE_ELEM(2, 1, state), \ BOOST_PP_TUPLE_ELEM(2, 1, seq))) #define SHIFTED_SUM(seq) \ BOOST_PP_TUPLE_ELEM(2, 0, \ BOOST_PP_SEQ_FOLD_LEFT( \ SHIFTED_SUM_OP, \ (BOOST_PP_SEQ_NIL, 0), \ seq)) SHIFTED_SUM(((A, 1)) ((B, 1)) ((C, 1)) ((D, 2)) ((E, 4))) In Christ, Steven Watanabe