diff --git a/README.md b/README.md index 78e83fa8..adf82d19 100644 --- a/README.md +++ b/README.md @@ -5694,6 +5694,22 @@ Generate a sequence of number from 'init' to 'end' ==> 1,2,3,4,5,6 +##### M\_REPLICATE(N, value) + +Replicate the value 'value' N times. + + M_REPLICATE(5, D) + ==> + D D D D D + +##### M\_REPLICATE\_C(N, value) + +Replicate the value 'value' N times, separating then by commas. + + M_REPLICATE_C(5, D) + ==> + D , D , D , D , D + ##### M\_NARGS(args...) Return the number of argument of the given list. diff --git a/m-core.h b/m-core.h index 949bd4ac..86ab5a7d 100644 --- a/m-core.h +++ b/m-core.h @@ -1567,6 +1567,14 @@ M_BEGIN_PROTECTED_CODE */ #define M_SEQ(init, end) M_MID_ARGS(init, M_INC(M_SUB(end, init)), 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52) +/* Replicate the value 'N' times */ +#define M_REPLICATE(N, value) \ + M_MAP2(M_RET_ARG1, value, M_SEQ(1, N)) + +/* Replicate the value 'N' times, separated by commas */ +#define M_REPLICATE_C(N, value) \ + M_MAP2_C(M_RET_ARG1, value, M_SEQ(1, N)) + /* Return the input (delay evaluation) */ #define M_ID(...) __VA_ARGS__ diff --git a/tests/test-mcore.c b/tests/test-mcore.c index 09f8f160..31cd27f3 100644 --- a/tests/test-mcore.c +++ b/tests/test-mcore.c @@ -305,6 +305,10 @@ static void test_list(void) assert(M_KEEP_ARGS(1, 1, 0, 0, 0, 0)); assert(M_SKIP_ARGS(4, 0, 0, 0, 0, 1)); assert(M_MID_ARGS(2, 1, 0, 0, 1, 0)); + const int tab[] = { M_REPLICATE_C(5, 1) }; + for(int i = 0; i < 5; i++) assert(tab[i] == 1); + const char str[] = M_REPLICATE(10, "N"); + assert(strcmp(str, "NNNNNNNNNN") == 0); } static void test_oplist(void)