Skip to content

About

C++ template designed to implement the functional programming concept of currying.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Latest commit

 

History

11 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 

Repository files navigation

Currying Class in C++

Overview

The Currying class is a C++ template designed to implement the functional programming concept of currying. It allows for partial application of function arguments, creating a new function with some arguments preset and others to be provided later.

Features

  • Flexible Argument Handling: Supports currying of functions with various argument types, including support for ownership control of arguments.
  • Ownership Control: Optional template parameter to specify whether the class should take ownership of the arguments.
  • Reference Qualifier Overloads: Overloaded operator() for both lvalue and rvalue references, allowing for optimized handling of temporary and non-temporary objects.

Requirements

  • C++20 or later (for requires, constexpr if, and other modern C++ features).

Usage

Basic Usage

Here's a simple example of how to use the Currying class:

#include "Currying.hpp"

int add(int a, int b) {
    return a + b;
}

int main() {
    auto curriedAdd = currying::Currying(add, currying::own_arguments, 5);
    int result = curriedAdd(10); // result is 15
    return 0;
}

In this example, curriedAdd is a curried version of the add function with the first argument preset to 5.

Ownership Control

You can control the ownership of the arguments:

  • ownArguments: The Currying class takes ownership of the arguments.
  • referenceArguments: The Currying class does not take ownership of the arguments.
auto curriedFunction = currying::Currying(add, currying::referenceArguments, 5);

API Reference

Constructors

  • Currying(FunctionType&& function, ArgOwnership<B> ownership, Args&&... arguments): Constructs a Currying object with the given function, ownership control, and arguments.

Member Functions

  • operator()(Args&&... args) &: Invokes the curried function with additional arguments for an lvalue Currying object.
  • operator()(Args&&... args) &&: Invokes the curried function with additional arguments for an rvalue Currying object.
  • getFunction() const: Returns the internal function.
  • getArguments() const: Returns the internally stored arguments.

Static Member Functions

  • argCount() noexcept: Returns the total number of arguments expected by the function.
  • argsFilled() noexcept: Returns the number of arguments already provided.
  • argsLeft() noexcept: Returns the number of arguments still required.

About

C++ template designed to implement the functional programming concept of currying.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages