Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

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

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
```