-
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* More samples.
- Loading branch information
Showing
11 changed files
with
240 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
#include "matchit.h" | ||
#include <iostream> | ||
#include <memory> | ||
|
||
struct Node { | ||
int value; | ||
std::unique_ptr<Node> lhs, rhs; | ||
}; | ||
|
||
bool operator==(Node const& lhs, Node const& rhs) | ||
{ | ||
return lhs.value == rhs.value && lhs.lhs == rhs.lhs && lhs.rhs == rhs.rhs; | ||
} | ||
|
||
void print_leftmost(const Node& node) { | ||
auto deref = [](auto&& e) -> decltype(auto) { return *e; }; | ||
using namespace matchit; | ||
Id<int> v; | ||
Id<Node> l; | ||
|
||
using N = Node; | ||
match(node) ( | ||
pattern | and_(app(&N::value, v), app(&N::lhs, nullptr)) = [&]{ std::cout << *v << '\n'; }, | ||
pattern | app(&N::lhs, app(deref, l)) = [&]{ print_leftmost(*l); } | ||
// ˆˆˆˆˆˆˆˆˆˆˆˆˆ dereference pattern | ||
); | ||
} | ||
|
||
|
||
int32_t main() | ||
{ | ||
const auto n = Node{4, {}, {}}; | ||
print_leftmost(n); | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
#include "matchit.h" | ||
#include <optional> | ||
#include <string_view> | ||
#include <iostream> | ||
|
||
template <typename T> | ||
struct Is { | ||
template <typename Arg> | ||
Arg&& operator()(Arg&& arg) const { | ||
static_assert(std::is_same_v<T, std::decay_t<Arg>>); | ||
return std::forward<Arg>(arg); | ||
} | ||
}; | ||
|
||
template <typename T> | ||
inline constexpr Is<T> is; | ||
|
||
auto sample1() | ||
{ | ||
const auto x = std::make_tuple(std::string("str"), 123); | ||
using namespace matchit; | ||
Id<std::string> s; | ||
Id<int> i; | ||
match(x)( | ||
pattern | ds(app(is<std::string>, s), app(is<int>, i)) = [&] { std::cout << "first " << *s << " second " << *i; } | ||
); | ||
std::cout << std::endl; | ||
} | ||
|
||
struct Email { | ||
constexpr std::optional<std::array<std::string_view, 2>> | ||
operator()(std::string_view sv) const | ||
{ | ||
auto const d = sv.find("@"); | ||
if (d == std::string_view::npos) | ||
{ | ||
return {}; | ||
} | ||
return std::array<std::string_view, 2>{sv.substr(0, d), sv.substr(d+1)}; | ||
} | ||
}; | ||
|
||
inline constexpr Email email; | ||
|
||
struct PhoneNumber { | ||
std::optional<std::array<std::string_view, 3>> | ||
operator()(std::string_view sv) const | ||
{ | ||
return std::array<std::string_view, 3>{sv.substr(0, 3), sv.substr(3, 3), sv.substr(6)}; | ||
} | ||
}; | ||
|
||
inline constexpr PhoneNumber phone_number; | ||
|
||
void sample2() | ||
{ | ||
using namespace matchit; | ||
|
||
using namespace std::literals; | ||
// auto const s = "match@it"sv; | ||
auto const s = "415123456"sv; | ||
Id<std::string_view> address, domain; | ||
match(s)( | ||
pattern | app(email, some(ds(address, domain))) = [&] { std::cout << "got an email"; }, | ||
pattern | app(phone_number, some(ds("415", _, _))) = [&] { std::cout << "got a San Francisco phone number"; } | ||
// ˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆ extractor pattern | ||
); | ||
std::cout << std::endl; | ||
} | ||
|
||
int32_t main() | ||
{ | ||
sample1(); | ||
sample2(); | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
#include "matchit.h" | ||
#include <iostream> | ||
#include <tuple> | ||
|
||
struct Player { std::string name; int hitpoints; int coins; }; | ||
|
||
void get_hint(const Player& p){ | ||
using namespace matchit; | ||
using P = Player; | ||
Id<std::string> n; | ||
match (p) ( | ||
pattern | app(&P::hitpoints, 1) = | ||
[&]{ std::cout << "You're almost destroyed. Give up!\n"; }, | ||
pattern | and_(app(&P::hitpoints, 10), app(&P::coins, 10)) = | ||
[&]{ std::cout << "I need the hints from you!\n"; }, | ||
pattern | app(&P::coins, 10) = | ||
[&]{ std::cout << "Get more hitpoints!\n"; }, | ||
pattern | app(&P::hitpoints, 10) = | ||
[&]{ std::cout << "Get more ammo!\n"; }, | ||
pattern | app(&P::name, n) = | ||
[&]{ | ||
if (*n != "The Bruce Dickenson") { | ||
std::cout << "Get more hitpoints and ammo!\n"; | ||
} else { | ||
std::cout << "More cowbell!\n"; | ||
} | ||
} | ||
); | ||
} | ||
|
||
int32_t main() | ||
{ | ||
const auto p = Player{"Bob", 4, 6}; | ||
get_hint(p); | ||
|
||
return 0; | ||
} |
Oops, something went wrong.