From f51f4e2127729f61570c1d1c03624119d95cffde Mon Sep 17 00:00:00 2001 From: Lucas Date: Fri, 10 Apr 2026 01:01:54 +0200 Subject: [PATCH] ajout toolbox.c --- functional_market/Fundamentals/toolbox.c | 137 ++++++++++++++++++++--- 1 file changed, 124 insertions(+), 13 deletions(-) diff --git a/functional_market/Fundamentals/toolbox.c b/functional_market/Fundamentals/toolbox.c index eb07661..2c306a3 100644 --- a/functional_market/Fundamentals/toolbox.c +++ b/functional_market/Fundamentals/toolbox.c @@ -1,4 +1,5 @@ #include "toolbox.h" +#include #include #include #include @@ -25,21 +26,24 @@ struct array *sort(struct array *arr, comparator f) free(temp); return arr; - } struct array *map(struct array *arr, size_t output_size, mapper f) -{ - +{ void *data = calloc(arr->len, output_size); if(!data) return NULL; size_t index = 0; while (index != arr->len){ - f(data[index], arr->data + index * arr->elem_size); + void *data2 = data + index * output_size; + void *n = arr->data + index * arr->elem_size; + + f(data2, n); index ++; } - memcpy(arr->data, data); + + free(arr->data); + arr->data = data; arr->elem_size = output_size; return arr; @@ -47,17 +51,53 @@ struct array *map(struct array *arr, size_t output_size, mapper f) struct array *filter(struct array *arr, predicate f) { - (void)arr; - (void)f; - return NULL; + size_t index = 0; + int flon = 0; + while(index != arr->len){ + + void *cur = arr->data + index * arr->elem_size; + + if(f(cur) != 0) flon ++; + + index ++; + } + + index = 0; + void *data1 = calloc(flon, arr->elem_size); + if(!data1 && flon > 0) return NULL; + size_t index2 = 0; + + while(index != arr->len){ + void *cur = arr->data + index * arr->elem_size; + + if(f(cur) != 0){ + void *n = data1 + index2 * arr->elem_size; + memcpy(n, cur, arr->elem_size); + index2 ++; + } + index ++; + } + + free(arr->data); + arr->data = data1; + arr->len = flon; + + return arr; } void *reduce(struct array *arr, void *start_value, reducer f) { - (void)arr; - (void)f; - (void)start_value; - return NULL; + void *rslt = start_value; + size_t index = 0; + + while(index != arr->len){ + void *cur = arr->data + index * arr->elem_size; + + rslt = f(rslt, cur); + index ++; + } + + return rslt; } void foreach(struct array *arr, acceptor f) @@ -71,7 +111,78 @@ void foreach(struct array *arr, acceptor f) index ++; } } +/* +#include +#include +int *add(int *res, int *n) +{ + *res += *n; + return res; +} + +void print_int(int *n) +{ + printf("%d\n", *n); +} + +int main(void) +{ + struct array arr = { + .data = calloc(4, sizeof(int)), + .len = 4, + .elem_size = sizeof(int), + }; + + int *data = arr.data; + data[0] = 0; + data[1] = 1; + data[2] = 2; + data[3] = 3; + + int res = 0; + reduce(&arr, &res, (reducer)&add); + printf("%d\n", res); + + free(arr.data); +} +*/ + +/* +#include +#include + +int odd(int *n) +{ + return *n % 2; +} + +void print_int(int *n) +{ + printf("%d\n", *n); +} + +int main(void) +{ + struct array arr = { + .data = calloc(4, sizeof(int)), + .len = 4, + .elem_size = sizeof(int), + }; + + int *data = arr.data; + data[0] = 0; + data[1] = 1; + data[2] = 2; + data[3] = 3; + + foreach(filter(&arr, (predicate)&odd), (acceptor)&print_int); + + free(arr.data); +} +*/ + +/* #include #include @@ -103,7 +214,7 @@ int main(void) free(arr.data); } - +*/ /*