[preprocessor] How to transform sequence into sequence of tuples.

How would I go about doing the following transformation: (a)(b)(c)(d) -> (a, b)(c, d) Thanks, Mostafa

On Wed, 10 Apr 2013 12:58:59 -0700, Mostafa
How would I go about doing the following transformation:
(a)(b)(c)(d) -> (a, b)(c, d)
Actually, I got the mapping crosswired. I want to transform a sequence into a tuple of "sequences", ie: (a)(b)(c)(d)(e)(f) -> (a b, c d, e f) I know there is BOOST_PP_SEQ_TO_TUPLE, and I know how to modify it's implementation to do what I want, but I rather use Boost.PP api's to achieve the desired the goal.

How would I go about doing the following transformation:
(a)(b)(c)(d) -> (a, b)(c, d)
Although, you probably could use BOOST_PP_SEQ_FOR_EACH, the easiest way is to use sequence iteration instead: #define ADJACENT_SEQ_TO_TUPLE(seq) BOOST_PP_CAT(ADJACENT_SEQ_TO_TUPLE_1 seq, _END) #define ADJACENT_SEQ_TO_TUPLE_0(x) x ADJACENT_SEQ_TO_TUPLE_2 #define ADJACENT_SEQ_TO_TUPLE_1(x) , x ADJACENT_SEQ_TO_TUPLE_2 #define ADJACENT_SEQ_TO_TUPLE_2(x) x ADJACENT_SEQ_TO_TUPLE_1 #define ADJACENT_SEQ_TO_TUPLE_0_END #define ADJACENT_SEQ_TO_TUPLE_1_END #define ADJACENT_SEQ_TO_TUPLE_2_END

On Thu, 11 Apr 2013 06:41:18 -0700, paul Fultz
How would I go about doing the following transformation:
(a)(b)(c)(d) -> (a, b)(c, d)
Although, you probably could use BOOST_PP_SEQ_FOR_EACH, the easiest way is to use sequence iteration instead:
#define ADJACENT_SEQ_TO_TUPLE(seq) BOOST_PP_CAT(ADJACENT_SEQ_TO_TUPLE_1 seq, _END) #define ADJACENT_SEQ_TO_TUPLE_0(x) x ADJACENT_SEQ_TO_TUPLE_2 #define ADJACENT_SEQ_TO_TUPLE_1(x) , x ADJACENT_SEQ_TO_TUPLE_2 #define ADJACENT_SEQ_TO_TUPLE_2(x) x ADJACENT_SEQ_TO_TUPLE_1 #define ADJACENT_SEQ_TO_TUPLE_0_END #define ADJACENT_SEQ_TO_TUPLE_1_END #define ADJACENT_SEQ_TO_TUPLE_2_END
Ah, thanks. I was going down the BOOST_PP_SEQ_FOR_EACH path. My algorithm was going to be #define SEQ (a)(b)(c)(d) #define WRAP(r, data, elem) \ LPAREN_IF(MOD(elem-index,2)) elem COMMA_IF(MOD(elem-index,2)) RPAREN_IF(NOT(MOD(elem-index,2))) SEQ_FOR_EACH(WRAP, ~, SEQ) But I couldn't figure out how to get the element index. BTW, what is the "next available BOOST_PP_FOR repetition" in the ternary macro for SEQ_FOR_EACH? For the example, why the is the output of: #define SEQ (w)(x)(y)(z)(a)(b)(c) #define MACRO(r, data, elem) r elem BOOST_PP_SEQ_FOR_EACH(MACRO, 1, SEQ) this: 2 w 3 x 4 y 5 z 6 a 7 b 8 c It seems r = elem-index + 2. Is this true? Thanks, Mostafa

Ah, thanks. I was going down the BOOST_PP_SEQ_FOR_EACH path. My algorithm was going to be
#define SEQ (a)(b)(c)(d) #define WRAP(r, data, elem) \ LPAREN_IF(MOD(elem-index,2)) elem COMMA_IF(MOD(elem-index,2)) RPAREN_IF(NOT(MOD(elem-index,2))) SEQ_FOR_EACH(WRAP, ~, SEQ)
But I couldn't figure out how to get the element index.
If you use SEQ_FOR_EACH_I, the element index will be passed in as an extra argument to WRAP.
BTW, what is the "next available BOOST_PP_FOR repetition" in the ternary macro for SEQ_FOR_EACH?
This is explained in Topics -> Reentrancy in the Boost.PP docs. Regards, Nate

On Thu, 11 Apr 2013 23:31:07 -0700, Nathan Ridge
Ah, thanks. I was going down the BOOST_PP_SEQ_FOR_EACH path. My algorithm was going to be
#define SEQ (a)(b)(c)(d) #define WRAP(r, data, elem) \ LPAREN_IF(MOD(elem-index,2)) elem COMMA_IF(MOD(elem-index,2)) RPAREN_IF(NOT(MOD(elem-index,2))) SEQ_FOR_EACH(WRAP, ~, SEQ)
But I couldn't figure out how to get the element index.
If you use SEQ_FOR_EACH_I, the element index will be passed in as an extra argument to WRAP.
Ah, thanks. (It would be very helpful if the distinction between SEQ_FOR_EACH_I and SEQ_FOR_EACH was emphasized in the former, preferably in the short description at the top of the reference page for SEQ_FOR_EACH_I.)
BTW, what is the "next available BOOST_PP_FOR repetition" in the ternary macro for SEQ_FOR_EACH?
This is explained in Topics -> Reentrancy in the Boost.PP docs.
Thanks again. (This again demonstrates the need for internal references/links in the documentation. One shouldn't have to read the *whole* documentation to effectively use parts of the library. This is very doable when parts of the library properly link to other relevant and dependent parts.) Thanks for the pointers. Mostafa
participants (3)
-
Mostafa
-
Nathan Ridge
-
paul Fultz