summaryrefslogtreecommitdiffhomepage
path: root/misc/examples/forloops.c
blob: 95a955a30c2b5e870d484cf52961ff29839d1395 (plain)
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <stdio.h>
#include <stc/views.h>

#define i_type IVec
#define i_val int
#include <stc/cstack.h>

#define i_type IMap
#define i_key int
#define i_val int
#include <stc/cmap.h>


int main()
{
    puts("c_FORRANGE:");
    c_FORRANGE (30) printf(" xx");
    puts("");

    c_FORRANGE (i, 30) printf(" %lld", i);
    puts("");

    c_FORRANGE (i, 30, 60) printf(" %lld", i);
    puts("");

    c_FORRANGE (i, 30, 90, 2) printf(" %lld", i);


    puts("\n\nc_forlist:");
    c_FORLIST (i, int, {12, 23, 453, 65, 676})
        printf(" %d", *i.ref);
    puts("");

    c_FORLIST (i, const char*, {"12", "23", "453", "65", "676"})
        printf(" %s", *i.ref);
    puts("");

    c_FORLIST (i, const char*, {"12", "23", "453", "65", "676"})
        printf(" %s", i.data[i.size - 1 - i.index]);


    c_AUTO (IVec, vec) 
    c_AUTO (IMap, map)
    {
        c_FORLIST (i, int, {12, 23, 453, 65, 113, 215, 676, 34, 67, 20, 27, 66, 189, 45, 280, 199})
            IVec_push(&vec, *i.ref);

        c_FORLIST (i, IMap_value, {{12, 23}, {453, 65}, {676, 123}, {34, 67}})
            IMap_push(&map, *i.ref);

        puts("\n\nc_foreach:");
        c_FOREACH (i, IVec, vec)
            printf(" %d", *i.ref);
        puts("");

        c_FOREACH (i, IMap, map)
            printf(" (%d %d)", i.ref->first, i.ref->second);

        puts("\n\nc_forpair:");
        c_FORPAIR (key, val, IMap, map)
            printf(" (%d %d)", *_.key, *_.val);

        puts("\n\nc_forwhile:");
        c_FORWHILE (i, IVec, IVec_begin(&vec), i.index < 3)
            printf(" %d", *i.ref);

        #define isOdd(i) (*i.ref & 1)

        puts("\n\nc_forfilter:");
        c_FORFILTER (i, IVec, vec
                      , c_FLT_SKIPWHILE(i, *i.ref != 65)
                     && c_FLT_TAKEWHILE(i, *i.ref != 280)
                     && c_FLT_SKIPWHILE(i, isOdd(i))
                     && isOdd(i)
                     && c_FLT_SKIP(i, 2)
                      , c_FLT_TAKE(i, 1))
            printf(" %d", *i.ref);
        puts("");
        // 189
    }
}