- You only need the
fpdirectory from theincludedirectory. Just copy it into your project and add to include path. - There are no dependencies; it just works.
- For examples of usage, take a look at the
*.cppfiles in thetestdirectory. - Use it responsibly and at your own discretion.
FP++ is organized to separate its core definitions, typeclasses, mixins, traits, prelude, and syntactic sugar.
Tdenotes a raw C++ type, such asconst int&ordouble*.Arepresents a normalized FP type, which is typically a wrapped or adapted version of a raw type.Fstands for type constructors, which are templates or higher-kinded types that produce types when applied.Fnusually indicates a unary arrow (function).
The following table summarizes the components involved in defining and using a
typeclass TC for a datatype F and an operation on F[A].
| Component | Location | Description |
|---|---|---|
| Core Typeclass | core/types/tc.h |
Provides TC<F>::operation |
| Instances | data/<type>.h |
Data types like Id, Option that implement typeclass instance methods. |
| Mixins | core/mixins/with_tc.h |
Given TC<F>, provides F<A>.operation as an instance method |
| Traits | traits/has_tc.h |
Defines concepts HasTC (constructible TC<F>) and IsTC (F<A> has .operation) |
| Prelude | prelude/operation.h |
Defines a free function operation on F[_] |
| Operators | operators/operation.h |
Provides syntactic sugar for the operation free function |
flowchart TD
Core[Core Typeclass<br/>core/types/tc.h]
Data[Instances<br/>data/type.h]
Mixin[Mixin<br/>core/mixins/with_tc.h]
Traits[Traits<br/>traits/has_tc.h]
Prelude[Prelude<br/>prelude/operation.h]
Operators[Operators<br/>operators/operation.h]
Core -->|provides methods to| Mixin
Mixin -->|adds instance methods to| Traits
Traits -->|used by free functions| Prelude
Prelude -->|used for syntactic sugar| Operators
Data -->|requires WithValue| Mixin
The mixins WithValue and WithApply, along with the free function pure, are
special in that they are not tied to a specific typeclass; instead, they must be
implemented by any datatype to enable storage and manipulation of values.
| Item | Provides |
|---|---|
WithValue |
Instance method .value() to extract the stored value |
WithApply |
Static internal method ::apply(fab) used by pure |
pure |
Free function pure<F>(a) to wrap a value |
For typeclass TC and TC::operation
core/types/tc.h: defineTC<F>with staticTC::operationcore/mixins/with_tc.h: add instance method.equalsvia mixinWithOperationwhich usesTC::operationtraits/has_tc.h: defineHasTCandIsTCconceptsprelude/operation.h: free functionoperationoperators/operation.h:operatorforoperation, if it makes sensetests/core/types/tc.cpp: test mixin and lawstests/prelude/operation.cpp: test freeoperationand operators
| core/types/tc.h | mixins/with_tc.h | traits/has_tc.h | prelude | Test Core | Test Prelude |
|---|---|---|---|---|---|
| Eq | WithEq | HasEq, IsEq | equals | Yes | Yes |
| Functor | WithMap | HasFunctor, IsFunctor | fmap |
flowchart LR
fp[fp]
fp -->|using| fp_core
fp_core -->|using| fp_core_data
fp_core -->|using| fp_core_types
subgraph FP
direction TB
fp_core[fp::core]
end
subgraph Core
direction TB
fp_core_data[fp::core::data]
fp_core_types[fp::core::types]
end
The directory structure under tests/ mirrors that of core/,
traits/, prelude/, and
operators/ under include/fp.
| Directory | Tests |
|---|---|
tests/core/types/TC.cpp |
Mixins, typeclass laws, traits |
tests/prelude/operation.cpp |
Free functions and operators |
- Clone this repository.
- Copy the
fpdirectory fromincludeinto your include path. - Include the library with
#include <fp/fp.h>. - Compile with a C++20-compliant compiler.
Requires a C++20-compatible compiler (tested with GCC 13 and Clang 16).
g++ -Iinclude -o fp_test main.cpp -std=c++20 -g -O0Documentation is incomplete. Most of the readme files are totally bogus.
cmake -B build -S .
cmake --build build
ctest --test-dir buildThis project is licensed under the MIT License - see the LICENSE file for details.
- Functional Programming in Scala, Second Edition by Paul Chiusano and Rúnar Bjarnason
- Haskell in Depth by Vitaly Bragilevsky
- Cats & Cats Effect