-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
executable file
·69 lines (63 loc) · 2.16 KB
/
Copy pathmain.cpp
File metadata and controls
executable file
·69 lines (63 loc) · 2.16 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
66
67
68
69
#include <iostream>
#include <vector>
#include "mixin.h"
using Mixin::mix;
using IterablePatch::ForEachIndexed, IterablePatch::Map, IterablePatch::Filter;
using std::cout, std::endl;
struct ObjStyleTest {
static void run() {
cout << "Object style syntax: mix things into an object." << endl;
std::vector<int> a{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
auto b = mix<ForEachIndexed, Map, Filter>::in(a);
auto c = b.filter([](auto &value) {
return value % 2 == 0;
}).map([](auto &value) {
return value * 10;
});
c.for_each_indexed([&c](auto idx, auto &n) {
cout << n << (idx < c.size() - 1 ? ", " : "");
});
cout << endl;
// Yes, it can be automatically cast back!
std::vector<int> d = c;
cout << "Testing implicit cast" << endl;
bool flag = true;
c.for_each_indexed([&flag, &d](auto idx, auto &v) {
flag &= v == d[idx];
});
cout << (flag ? "YES!" : "NO!") << endl << endl;
}
};
struct ClassStyleTest {
template<typename T>
using vector = typename mix<ForEachIndexed, Map, Filter>::in_class<std::vector<T>>;
static void run() {
cout << "Class style syntax: mix things into a class" << endl;
// Normal constructors and list initializer are all supported.
vector<int> a{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
// vector<int> a(3, 10);
// vector<int> a(2);
auto b = a.filter([](auto &value) {
return value % 2 == 0;
}).map([](auto &value) {
return value * 10;
});
b.for_each_indexed([&b](auto idx, auto &value) {
cout << value << (idx < b.size() - 1 ? ", " : "");
});
cout << endl;
// Yes, it can be automatically cast back!
std::vector<int> c = b;
cout << "Testing implicit cast" << endl;
bool flag = true;
b.for_each_indexed([&b, &flag](auto idx, auto &value) {
flag &= value == b[idx];
});
cout << (flag ? "YES!" : "NO!") << endl << endl;
}
};
int main() {
ObjStyleTest::run();
ClassStyleTest::run();
return 0;
}