Introduction to FunctionalPlus
masterstd::vector, std::list, std::string) and custom containers that provide a similar interface.repository·master·Indexed 25 days ago
https://github.com/dobiasd/functionalplusA header-only C++14 library providing functional programming utilities to reduce boilerplate and improve code readability. It offers tools for container manipulation, function composition, and forward application, including functions like fplus::keep_if, fplus::all_the_same, fplus::maximum_on, and fplus::generate, as well as a pipe-style programming interface via the fplus::fwd namespace.
std::vector, std::list, std::string) and custom containers that provide a similar interface.The fplus::fwd namespace provides partially curried versions of the standard fplus functions. This enables a 'pipe' style of programming where data is passed through a chain of functions without the need for intermediate temporary variables. This is achieved using fplus::fwd::apply or by composing functions with fplus::fwd::compose (point-free style).
// Forward application style (pipe-like)
return fwd::apply(
input
, fwd::split_lines(false)
, fwd::transform(convert_container<characters, std::string>)
, fwd::fold_left_1(set_intersection<characters>)
, fwd::size_of_cont()
, fwd::show()
);
// Point-free style (composition)
const auto gemstone_count_fwd_compose = fwd::compose(
fwd::split_lines(false),
fwd::transform(convert_container<characters, std::string>),
fwd::fold_left_1(set_intersection<characters>),
fwd::size_of_cont(),
fwd::show()
);FunctionalPlus is a header-only library. It requires a C++14 compatible compiler. Supported compilers include:
You can use ExternalProject_Add to manage FunctionalPlus as an external dependency. This method requires no system installation and allows you to manage the source directory manually.
cmake_minimum_required(VERSION 3.14)
project(FplusMinimalExternalExample)
include(ExternalProject)
ExternalProject_Add(
functional_plus
GIT_REPOSITORY https://github.com/Dobiasd/FunctionalPlus.git
GIT_TAG master
SOURCE_DIR "${CMAKE_BINARY_DIR}/thirdparty/fplus"
CONFIGURE_COMMAND ""
BUILD_COMMAND ""
INSTALL_COMMAND ""
LOG_DOWNLOAD ON
LOG_BUILD ON
)
ExternalProject_Get_Property(functional_plus SOURCE_DIR)
add_executable(main src/main.cpp)
add_dependencies(main functional_plus)
target_compile_features(main PRIVATE cxx_std_14)
target_include_directories(main PRIVATE "${SOURCE_DIR}")Add FunctionalPlus to your conanfile.txt and run conan install.
[requires]
functionalplus/0.2.28
[generators]
cmakeconan install conanfile.txtThe preferred method for automatic downloading and integration within CMake is using FetchContent. This avoids manual installation and allows for better version control via GIT_TAG.
cmake_minimum_required(VERSION 3.14)
project(FplusMinimalExternalExample)
include(FetchContent)
FetchContent_Declare(
functional_plus
GIT_REPOSITORY https://github.com/Dobiasd/FunctionalPlus.git
GIT_TAG master
)
FetchContent_MakeAvailable(functional_plus)
add_executable(main src/main.cpp)
target_compile_features(main PRIVATE cxx_std_14)
target_link_libraries(main PRIVATE FunctionalPlus::fplus)For a lightweight setup, you can use the standalone fplus.hpp header which groups all FunctionalPlus code into a single file. This is useful for manual compilation or using Compiler Explorer (godbolt.org).
To download the header:
mkdir -p external/fplus/include/fplus
wget -O external/fplus/include/fplus/fplus.hpp https://raw.githubusercontent.com/Dobiasd/FunctionalPlus/master/include_all_in_one/include/fplus/fplus.hppTo compile with g++:
g++ --std=c++14 src/main.cpp -Iexternal/fplus/includeTo use with CMake:
cmake_minimum_required(VERSION 3.14)
project(YourProjectName)
add_executable(main src/main.cpp)
target_compile_features(main PRIVATE cxx_std_14)
target_include_directories(main PRIVATE external/fplus/include)To use in Compiler Explorer:
#include "https://raw.githubusercontent.com/Dobiasd/FunctionalPlus/master/include_all_in_one/include/fplus/fplus.hpp"Install using the conda package manager from the conda-forge channel.
conda config --add channels conda-forge
conda install FunctionalPlusYou can install FunctionalPlus manually using CMake by cloning the repository, building it, and installing it to your system.
To build and install:
git clone https://github.com/Dobiasd/FunctionalPlus
cmake -S FunctionalPlus -B FunctionalPlus/build
cmake --build FunctionalPlus/build
sudo cmake --install FunctionalPlus/buildOnce installed, add it to your own CMake project using find_package and link against the FunctionalPlus::fplus target.
Install using Homebrew:
brew install functionalplusIf you are not using CMake, you may need to add $(brew --prefix functionalplus)/include to your compiler's additional include paths.
Use cget to install FunctionalPlus. Ensure you initialize the toolchain to use C++14.
# Setup up toolchain to use c++14
cget init --std=c++14
# Test and install
cget install Dobiasd/FunctionalPlusUse the vcpkg dependency manager to install the fplus port.
git clone https://github.com/Microsoft/vcpkg.git
cd vcpkg
./bootstrap-vcpkg.sh
./vcpkg integrate install
./vcpkg install fplus