-
Notifications
You must be signed in to change notification settings - Fork 2
/
iterable_utils.c
42 lines (36 loc) · 1.13 KB
/
iterable_utils.c
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
#include "iterable_utils.h"
#include "../func_iter.h"
#include <stdio.h>
#include <string.h>
/*
* Definitions of generic functions that work on an Iterator instance, i.e Iterable
*
* These functions don't need to care about the exact data structure backing the iterator
*
* At the same time, the Iterator instance is lazy - so the data isn't pre-generated and put into
* a common struct, instead - it's extracted from the actual source backing the iterator on demand
*/
/* Generic function to sum values from any iterable yielding int */
int sum_intit(Iterable(int) it)
{
int sum = 0;
foreach (int, x, it) {
sum += x;
}
return sum;
}
/* Generic function to print values from any iterable yielding string */
void print_strit(Iterable(string) it)
{
foreach (string, s, it) {
printf("%s ", s);
}
puts("");
}
// clang-format off
/* Implement `take` functionality for uint32_t iterables */
define_itertake_func(uint32_t)
/* Implement `map` functionality for int -> int iterables */
define_itermap_func(int, int)
/* Implement `map` functionality for int -> char* iterables */
define_itermap_func(int, string)