Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/briancairl/zen
A functional/railway-oriented programming library for C++17
https://github.com/briancairl/zen
cpp cpp17 cpp20 functional-programming header-only multithreading railway-oriented-programming
Last synced: 18 days ago
JSON representation
A functional/railway-oriented programming library for C++17
- Host: GitHub
- URL: https://github.com/briancairl/zen
- Owner: briancairl
- License: mit
- Created: 2022-09-23T04:46:03.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2022-11-15T16:08:17.000Z (about 2 years ago)
- Last Synced: 2024-03-17T10:35:27.311Z (10 months ago)
- Topics: cpp, cpp17, cpp20, functional-programming, header-only, multithreading, railway-oriented-programming
- Language: C++
- Homepage:
- Size: 513 KB
- Stars: 5
- Watchers: 3
- Forks: 0
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
README
[![Unit Tests](https://github.com/briancairl/zen/actions/workflows/pr.yml/badge.svg)](https://github.com/briancairl/zen/actions/workflows/pr.yml)
# Zen
Zen provides facilities for railroad style programming in C++.
This library supports parallel execution which can be easily swapped into any single-threaded program written with Zen.
## API Documentation
Documentation for latest version available [here](https://briancairl.github.io/zen/doxygen-out/html/index.html).
## Examples
### Simple sequence
```c++
#include#include
int main(int argc, char** argv)
{
using namespace zen;auto r = pass(argc, argc)
| [](int a, float b) -> result { return a + b; }
| [](float b) -> result { return 2.f * b; };if (r.valid())
{
std::cout << "r: " << *r << std::endl;
}
else
{
std::cout << r.status() << std::endl;
}
};
```### Merging with `any` and `all`
```c++
#include#include
int main(int argc, char** argv)
{
using namespace zen;
auto r = pass(argc, argc)
| [](int a, float b) -> result
{
if (a > 2)
{
// Failure message
return "invalid 1 "_msg;
}
return 2 * b;
}
| any(
[](float a) -> result { return 2 * a; },
[](float a) -> result
{
if (a > 2)
{
// Failure message
return "invalid 2"_msg;
}
return 2 * a;
})
| all(
[](float a) -> result { return 2 * a; },
[](float a) -> result
{
if (a > 2)
{
// Failure message
return "invalid 3"_msg;
}
return 2 * a;
});
if (r.valid())
{
const auto [a, b] = *r;
std::cout << "a: " << a << std::endl;
std::cout << "b: " << b << std::endl;
}
else
{
std::cout << r.status() << std::endl;
}
};
```### Easy multi-threading with `any` and `all`
```c++
#include#include
int main(int argc, char** argv)
{
using namespace zen;exec::thread_pool tp{4};
auto r = pass(argc, argc)
| [](int a, float b) -> result { return 2 * b; }
| any(
tp,
[](float a) -> result { return 2 * a; },
[](float a) -> result { return 2 * a; })
| all(
tp,
[](float a) -> result { return 2 * a; },
[](float a) -> result { return 3 * a; },
[](float a) -> result { return 4 * a; });
if (r.valid())
{
const auto [a, b, c] = *r;
std::cout << "a: " << a << std::endl;
std::cout << "b: " << b << std::endl;
std::cout << "c: " << c << std::endl;
}
else
{
std::cout << r.status() << std::endl;
}
};
```# Running examples
```
bazel run examples:
``````
bazel run examples:zen
```# Running tests
## bazel
```
bazel test test/... --test_output=all --cache_test_results=no --compilation_mode=dbg
```