FunctionalPlus

repository·master·Indexed 25 days ago

https://github.com/dobiasd/functionalplus

A 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.

Tokens
3.2K
Snippets
16
Records
20
Agent score
81%

What's inside FunctionalPlus

  1. Introduction to FunctionalPlus

    master
    FunctionalPlus is a small, header-only C++ library designed to reduce code noise and improve readability by providing pure, easy-to-use functions for common control flows. It allows developers to move away from low-level iterators and hand-written loops toward a more functional style, focusing on high-level abstractions. It works with standard STL containers (like std::vector, std::list, std::string) and custom containers that provide a similar interface.
  2. How forward application works in `fplus::fwd`

    master

    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()
    );
  3. Integrate FunctionalPlus using CMake ExternalProject

    master

    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}")
  4. Integrate FunctionalPlus using CMake FetchContent

    master

    The 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)
  5. Use the FunctionalPlus 'all-in-one' header

    master

    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.hpp

    To compile with g++:

    g++ --std=c++14 src/main.cpp -Iexternal/fplus/include

    To 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"
  6. Install FunctionalPlus via CMake

    master

    You 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/build

    Once installed, add it to your own CMake project using find_package and link against the FunctionalPlus::fplus target.