-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathRest-pattern.cpp
71 lines (62 loc) · 2.36 KB
/
Rest-pattern.cpp
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
#include "matchit.h"
#include <iostream>
#include <vector>
using namespace matchit;
template <typename T>
std::ostream &operator<<(std::ostream &o, Subrange<T> const &subrange)
{
o << " [ ";
for (auto const &e : subrange)
{
o << e << ", ";
}
o << " ] ";
return o;
}
void sample()
{
auto const words = std::vector<std::string>{"a", "b", "c"};
auto const &slice = words;
Id<std::string> head;
Id<SubrangeT<std::vector<std::string> const>> tail;
match(slice)(
// clang-format off
pattern | ds() = [&] { std::cout << "slice is empty" << std::endl; },
pattern | ds(head) = [&] { std::cout << "single element " << *head << std::endl; },
pattern | ds(head, tail.at(ooo)) = [&] { std::cout << "head=" << *head << " tail=" << *tail << std::endl; }
// clang-format on
);
Id<SubrangeT<std::vector<std::string> const>> subrange;
match(slice)(
// clang-format off
// Ignore everything but the last element, which must be "!".
pattern | ds(ooo, "!") = [&] { std::cout << "!!!" << std::endl; },
// `subrange` is a slice of everything except the last element, which must be "z".
pattern | ds(subrange.at(ooo), "z") = [&] { std::cout << "starts with: " << *subrange << std::endl; },
// `subrange` is a slice of everything but the first element, which must be "a".
pattern | ds("a", subrange.at(ooo)) = [&] { std::cout << "ends with: " << *subrange << std::endl; },
pattern | ds(subrange.at(ooo)) = [&] { std::cout << *subrange << std::endl; }
// clang-format on
);
Id<std::string> penultimate;
match(slice)(
// clang-format off
pattern | ds(ooo, penultimate, _) = [&] { std::cout << "next to last is " << *penultimate << std::endl; }
// clang-format on
);
constexpr auto tuple = std::make_tuple(1, 2, 3, 4, 5);
// Rest patterns may also be used in tuple and tuple struct patterns.
Id<int32_t> y, z;
match(tuple)(
// clang-format off
pattern | ds(1, ooo, y, z) = [&] { std::cout << "y=" << *y << " z=" << *z << std::endl; },
pattern | ds(ooo, 5 ) = [&] { std::cout << "tail must be 5" << std::endl; },
pattern | ds(ooo ) = [&] { std::cout << "matches everything else" << std::endl; }
// clang-format on
);
}
int32_t main()
{
sample();
return 0;
}