From b918f61f4f3ab41ca0a912aaece2c5ebb40a2518 Mon Sep 17 00:00:00 2001 From: Patrick Pelissier Date: Sun, 26 Feb 2023 23:26:51 +0100 Subject: [PATCH] Update example --- example/ex-algo04.c | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/example/ex-algo04.c b/example/ex-algo04.c index 14c9a701..d1a07641 100644 --- a/example/ex-algo04.c +++ b/example/ex-algo04.c @@ -7,28 +7,31 @@ ARRAY_DEF(vector_int, int) /* Register the oplist of this array of int globally */ #define M_OPL_vector_int_t() ARRAY_OPLIST(vector_int) -/* Set s (string_t) to the integer i */ -#define str_ui(s, i) string_printf(s, "%d", i) - /* Concat two strings by insertion a '-' within them */ #define str_cat(s1, s2) (string_cat_str(s1, "-"), string_cat(s1, s2)) int main(void) { - // Let s be a string_t and initilize it. + // Let 's' be a string_t and initialize it. M_LET(s, string_t) - // Let v be a vector of integer and initialize it. + // Let 'v' be a vector of integer and initialize it. M_LET( (v, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10), vector_int_t) { int sum = 0, product = 0; // Compute the sum of theses integers. + // 'add' is a registered keyword, recognized by M*LIB to perform a sum. ALGO_REDUCE(sum, v, vector_int_t, add); // Compute the product of theses integers. + // 'mul' is a registered keyword, recognized by M*LIB to perform a product. ALGO_REDUCE(product, v, vector_int_t, mul); - /* Example of reduction with a transformation of the type from int to string_t */ - ALGO_REDUCE( (s, string_t), v, vector_int_t, str_cat, str_ui); + /* Example of reduction with a transformation of the type from int to string_t + By default, the result ("s") should be of the same type as the base type + of the container. This is not the case here, we want to transform the + type. As such, we add the precision of the type 'string_t' to 's'. + The transformation from string to integer is done by string_set_si */ + ALGO_REDUCE( (s, string_t), v, vector_int_t, str_cat, string_set_si); printf ("sum: %d\n" "product: %d\n"