This repository contains my attempt at implementing functional patterns in C++.
I attempt to make the code as simple as possible which sometimes means that
examples might contain some copies that can possibly be avoided in some, possibly large amount of code.
Most of the examples are usable in constexpr context.
Examples also contain a link to the same code in compiler explorer (where applicable). Console output in compiler explorer links is replaced with return so that you can clearly see that the sample is inlined.
note: all of the examples are tested using latest stable clang build.
They should work in gcc and they might work in msvc.
If something does not work in gcc for some weird reason let me know (and raise an issue to the gcc and/or clang team depending on who is not conforming to the c++ standard this time).
You need cmake (minimum version 3.13) and a clang with c++17 support (version 5 or latter)
mkdir build && cd build
CXX=$(which clang++) cmake ..
make
./[name of the executable]
each .cpp file creates a separate executable so that all of the examples can be tested individually without effecting each other.
Currently the following examples are implemented:
-
the art of combining (chaining) functions to build more complicated ones.
-
The mechanism that allows to invoke a function with only a part of its arguments, producing a function with the rest of the arguments.
note 1: this is similar tostd::bind
(or actually c++20'sstd::bind_front
) however much faster and constexpr. Also don't usestd::bind
.
note 2: if you really need to bind to some specific parameters of a function just use a lambda. -
Separating a function of n arguments into a sequence of n functions each with a single argument.
This implementation separates a function of p arguments into some number of functions n with pi arguments such that Σni=1( pi ) = p.
It will make sense when you will see the example.
Credit to here for inspiration. -
pattern matching for
std::variant
andstd::optional
This one uses naming to make it look like pattern matching is supported on language level