https://github.com/mnikander/cpp_function_lifting
Function lifting to automatically extend unary and binary functions to vector-valued inputs
https://github.com/mnikander/cpp_function_lifting
Last synced: 10 months ago
JSON representation
Function lifting to automatically extend unary and binary functions to vector-valued inputs
- Host: GitHub
- URL: https://github.com/mnikander/cpp_function_lifting
- Owner: mnikander
- License: bsd-3-clause
- Created: 2024-06-14T04:58:55.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-06-15T11:18:00.000Z (over 1 year ago)
- Last Synced: 2024-11-10T15:41:43.957Z (about 1 year ago)
- Language: C++
- Homepage:
- Size: 15.6 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Function lifting
Allow binary functions operating on two elements, to be applied to two sequences of elements, without having to explicitly call transform and without having to implement extra overloads for each function.
## Getting started
1. Install dependencies
```bash
sudo apt get install libgtest-dev cmake g++
```
2. Clone the repo
```bash
git clone #...
```
3. Build and run:
```bash
# out-of-source build
cd cpp_function_lifting && mkdir out && cd out && cmake .. && cd ..
cmake --build out/ && ./out/unit_tests
```
You can find plenty of usage examples in the unit tests, but here are a few...
## Examples for unary functions
```cpp
// unary function
// Negate: scalar -> scalar
const int a = 2;
const int r = fl::Negate{}(a); // -2
```
'Negate' can be lifted automatically:
```cpp
// Negate: vector -> vector
const std::vector a = {-1, 0, 1};
const std::vector r = fl::Negate{}(a); // [1, 0, -1]
```
## Examples for binary functions
```cpp
// binary function
// Add: (scalar, scalar) -> scalar
const int a = 2;
const int b = 3;
const int r = fl::Add{}(a, b); // 5
```
'Add' can be lifted automatically:
```cpp
// Add: (scalar, vector) -> vector
const int a = 10;
const std::vector b = {1, 2};
const std::vector r = fl::Add{}(a, b); // [11, 12]
```
```cpp
// Add: (scalar, vector) -> vector
const std::vector a = {1, 2};
const int b = 10;
const std::vector r = fl::Add{}(a, b); // [11, 12]
```
```cpp
// Add: (vector, vector) -> vector
const std::vector a = {10, 20};
const std::vector b = {1, 2};
const std::vector r = fl::Add{}(a, b); // [11, 22]
```