From 72685517372587b8f3cdcb59d6029fcb017a07ea Mon Sep 17 00:00:00 2001 From: Patrick Pelissier Date: Sun, 13 Mar 2022 18:53:35 +0100 Subject: [PATCH] Add micro utilities macros for handling list --- m-core.h | 13 +++++++++++++ tests/test-mcore.c | 10 ++++++++++ 2 files changed, 23 insertions(+) diff --git a/m-core.h b/m-core.h index 4a504124..9b2d3bc5 100644 --- a/m-core.h +++ b/m-core.h @@ -1675,6 +1675,19 @@ M_BEGIN_PROTECTED_CODE #define M_FILTER_00(d, item) \ M_IF(M_PAIR_1 d(M_PAIR_2 d, item))(item, ) +/* Return the HEAD element of a list */ +#define M_HEAD(x, ...) x + +/* Return the HEAD element of the tail of a list */ +#define M_HEAD_2(x, y, ...) y + +/* Return the tail sublist of the list (removing the HEAD) */ +#define M_TAIL(x, ...) __VA_ARGS__ + +/* Return the tail sublist of the sublist (removing the 2 first elements) */ +#define M_TAIL_2(x, y, ...) __VA_ARGS__ + + /* Concatene two arglists 'a' and 'b', handling the case of empty arglist handling the cases where the arglists are within parenthesis. diff --git a/tests/test-mcore.c b/tests/test-mcore.c index 872f4239..9e8f9b82 100644 --- a/tests/test-mcore.c +++ b/tests/test-mcore.c @@ -758,6 +758,15 @@ static void test_properties(void) assert( M_GET_PROPERTY (OP_PROP(), IT_TYPE ) == 2); assert( M_KEYWORD_P( M_GET_PROPERTY (OP_PROP(), INIT_WITH ), priority)); } + +static void test_generic_api(void) +{ + assert( M_HEAD(17, 0) == 17); + assert( M_TAIL(18, 19) == 19); + assert( M_HEAD_2(18, 17, 0) == 17); + assert( M_TAIL_2(18, 17, 8) == 8); +} + int main(void) { test_cat(); @@ -783,5 +792,6 @@ int main(void) test_str_hash(); test_M_CSTR(); test_properties(); + test_generic_api(); exit(0); }