-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathiterable_utils.h
34 lines (26 loc) · 1.49 KB
/
iterable_utils.h
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
#ifndef IT_ITRBLE_UTILS_H
#define IT_ITRBLE_UTILS_H
#include "../func_iter.h"
#include "map.h"
#include "take.h"
#define UNIQVAR(x) CONCAT(CONCAT(x, _4x2_), __LINE__) /* "Unique" variable name */
/* Iterate through given `it` iterable that contains elements of type `T` - store each element in `x` */
#define foreach(T, x, it) \
Maybe(T) UNIQVAR(res) = (it).tc->next((it).self); \
for (T x = from_just_(UNIQVAR(res)); is_just(UNIQVAR(res)); \
UNIQVAR(res) = (it).tc->next((it).self), x = from_just_(UNIQVAR(res)))
/* Implement `IterTake` struct for uint32_t iterables */
DefineIterTake(uint32_t);
/* Implement `IterMap` struct for int -> int iterables */
DefineIterMap(int, int);
/* Implement `IterMap` struct for int -> char* iterables */
DefineIterMap(int, string);
/* Generic function to sum values from any iterable yielding int */
int sum_intit(Iterable(int) it);
/* Generic function to print values from any iterable yielding string */
void print_strit(Iterable(string) it);
/* Make an iterable of the first n elements of given iterable */
Iterable(uint32_t) prep_itertake_of(uint32_t)(IterTake(uint32_t) * x);
Iterable(int) prep_itermap_of(int, int)(IterMap(int, int) * x);
Iterable(string) prep_itermap_of(int, string)(IterMap(int, string) * x);
#endif /* !IT_ITRBLE_UTILS_H */