-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathitplus_fold.h
More file actions
65 lines (61 loc) · 2.75 KB
/
itplus_fold.h
File metadata and controls
65 lines (61 loc) · 2.75 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
/**
* @file
* @brief Macros for implementing the `foldl` abstraction.
*
* https://hackage.haskell.org/package/base-4.15.0.0/docs/Data-List.html#v:foldl
*/
#ifndef LIB_ITPLUS_FOLD_H
#define LIB_ITPLUS_FOLD_H
#include "itplus_foreach.h"
#include "itplus_iterator.h"
/**
* @def define_iterfold_func(T, Acc, Name)
* @brief Define the `fold` function for an iterable and an accumulator type.
*
* The defined fold function takes in an iterable of type `T`, a function of type `Acc (*const f)(Acc acc, T x)`, and
* a starting value of type `Acc`, and folds the iterable to a singular value of type `Acc`, by repeatedly applying `f`
* onto it.
*
* This defined function will consume the given iterable.
*
* # Example
*
* @code
* typedef struct boxint { int x; } BoxInt;
*
* // The defined function has the signature:-
* // `BoxInt int_boxint_fold(Iterable(int), BoxInt init, BoxInt (*f)(BoxInt acc, int x))`
* define_iterfold_func(int, boxint, int_boxint_fold)
* @endcode
*
* Usage of the defined function-
*
* @code
* // Add 2 ints within 2 BoxInts
* static BoxInt boxed_add(BoxInt a, BoxInt b) { return (BoxInt){ .x = a.x + b.x }; }
* @endcode
*
* @code
* // Fold `it` (of type `Iterable(int)`) with `boxed_add`
* BoxInt boxed_sum = int_boxint_fold(it, (BoxInt){0}, boxed_add);
* @endcode
*
* @param T The type of value the `Iterable`, for which this is being implemented, yields.
* @param Acc The accumulator type the fold function being defined should work on.
* @param Name Name to define the function as.
*
* @note If `T` (or `Acc`) is a pointer, it needs to be typedef-ed into a type that does not contain the `*`. Only
* alphanumerics.
* @note An #Iterator(T) for the given `T` **must** also exist.
* @note This should not be delimited with a semicolon.
*/
#define define_iterfold_func(T, Acc, Name) \
Acc Name(Iterable(T) it, Acc init, Acc (*f)(Acc acc, T x)) \
{ \
Acc acc = init; \
foreach (T, x, it) { \
acc = f(acc, x); \
} \
return acc; \
}
#endif /* !LIB_ITPLUS_FOLD_H */