https://github.com/sentomk/patternia
Providing pattern matching for modern c++.
https://github.com/sentomk/patternia
cpp cross-platform pattern-matching
Last synced: 14 days ago
JSON representation
Providing pattern matching for modern c++.
- Host: GitHub
- URL: https://github.com/sentomk/patternia
- Owner: sentomk
- License: mit
- Created: 2025-08-05T12:10:00.000Z (8 months ago)
- Default Branch: main
- Last Pushed: 2026-03-10T15:01:44.000Z (17 days ago)
- Last Synced: 2026-03-10T21:27:02.676Z (16 days ago)
- Topics: cpp, cross-platform, pattern-matching
- Language: C++
- Homepage: https://patternia.tech/
- Size: 3.41 MB
- Stars: 129
- Watchers: 0
- Forks: 4
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
- Security: SECURITY.md
Awesome Lists containing this project
README
[](https://isocpp.org/)
[](https://github.com/SentoMK/patternia/actions)
[](LICENSE)
[](https://github.com/SentoMK/patternia/releases)
[](https://patternia.tech)
**Patternia** is a header-only pattern matching library for modern C++.
It keeps matching expression-oriented, explicit, and zero-overhead.
## Syntax
```cpp
#include
int classify(int x) {
using namespace ptn;
return match(x) | on(
lit(0) >> 0,
lit(1) >> 1,
_ >> -1
);
}
```
`match(subject)` creates the evaluation context.
`on(...)` provides the ordered case list.
`pattern >> handler` defines one case.
`_` is the required fallback case.
## Highlights
- Literal, structural, and `std::variant` matching in one DSL.
- Explicit binding through `$` and `$(...)`.
- Declarative guards via `PTN_LET`, `PTN_WHERE`, `_0`, `arg`, `rng(...)`, and callables.
- No RTTI, no virtual dispatch, no heap allocation.
- Static literal and variant dispatch lowering for hot paths.
## Quick Examples
### Guarded value match
```cpp
using namespace ptn;
const char *bucket(int x) {
return match(x) | on(
$[PTN_LET(value, value < 0)] >> "negative",
$[PTN_LET(value, value < 10)] >> "small",
_ >> "large"
);
}
```
### Structural match
```cpp
using namespace ptn;
struct Point { int x; int y; };
int magnitude2(const Point &p) {
return match(p) | on(
$(has<&Point::x, &Point::y>()) >> [](int x, int y) {
return x * x + y * y;
},
_ >> 0
);
}
```
### Variant match
```cpp
using namespace ptn;
using Value = std::variant;
std::string describe(const Value &v) {
return match(v) | on(
is() >> "int",
$(is()) >> [](const std::string &s) {
return "str:" + s;
},
_ >> [] { return std::string("other"); }
);
}
```
## Installation
Patternia is header-only with no external dependencies.
**vcpkg** (recommended):
```bash
vcpkg install patternia
```
```cmake
find_package(patternia CONFIG REQUIRED)
target_link_libraries(your_target PRIVATE patternia::patternia)
```
**FetchContent**:
```cmake
include(FetchContent)
FetchContent_Declare(patternia
GIT_REPOSITORY https://github.com/sentomk/patternia.git
GIT_TAG v0.9.0
)
FetchContent_MakeAvailable(patternia)
target_link_libraries(your_target PRIVATE patternia::patternia)
```
**Direct clone**:
```bash
git clone https://github.com/SentoMK/patternia.git
cd patternia
cmake -S . -B build
cmake --build build
```
See [Installation Guide](https://patternia.tech/guide/installation/) for `find_package`, submodule, and header-copy options.
## Tests
```bash
cmake -S . -B build -DPTN_BUILD_TESTS=ON
cmake --build build --target ptn_tests
ctest --test-dir build --output-on-failure
```
## Performance-Oriented Usage
Cache the case pack for repeated hot paths:
```cpp
using namespace ptn;
int fast_classify(int x) {
return match(x) | PTN_ON(
lit<1>() >> 1,
lit<2>() >> 2,
_ >> 0
);
}
```
`PTN_ON(...)` is a convenience wrapper over `static_on(...)`.
It avoids rebuilding the matcher object on every call.
## Documentation
- [Getting Started](https://patternia.tech/guide/getting-started/)
- [API Reference](https://patternia.tech/api/)
- [Design Overview](https://patternia.tech/design-overview/)
- [Tutorials](https://patternia.tech/tutorials/from-control-flow/)
- [Performance Notes](https://patternia.tech/performance/)
## Contributing
Please read [CONTRIBUTING.md](CONTRIBUTING.md) before sending changes.
This project is governed by [CODE_OF_CONDUCT.md](CODE_OF_CONDUCT.md).