All Classes Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
Functional module

Provides support for functional programming. More...

Modules

 Function objects submodule
 Provides function objects to use.
 

Functions

template<typename First , typename... Rest>
constexpr detail::Composer
< First, Rest...> 
gears::functional::compose (First &&f, Rest &&...args)
 Function composition of functions. More...
 
template<typename Function , typename... Args>
constexpr curry_type< Function,
detail::SpecialDecay< Args >...> 
gears::functional::curry (Function &&f, Args &&...args)
 Applies function currying to functions. More...
 
template<typename... Args>
constexpr Result gears::functional::invoke (Args &&...args) noexcept(NoExcept)
 Implements the INVOKE facility in the C++ standard. More...
 

Detailed Description

This module provides multiple function objects (also called functors) and operations that make working in a functional manner easier.

All of the function objects have a constexpr instantiation to allow working with the other components of the module easier. It also allows the user to use the function objects just like a regular function.

There's also basic support for currying and composition. More support for other things are coming soon.

Example usage:

#include <gears/functional.hpp>
#include <iostream>
namespace fn = gears::functional;
int main() {
// equivalent to
// fn::even(fn::plus(...))
std::cout << f(11, 15);
}

Output:

1

Function Documentation

template<typename First , typename... Rest>
constexpr detail::Composer<First, Rest...> gears::functional::compose ( First &&  f,
Rest &&...  args 
)

Enables function composition of multiple functions. Note that the syntax goes left to right, so for example:

compose(f, g, h);

would be equivalent to f(g(h()))

Parameters
fFirst function to compose
argsRest of the functions to compose
Returns
A function object that allows you to call the composed function.

Definition at line 82 of file compose.hpp.

template<typename Function , typename... Args>
constexpr curry_type<Function, detail::SpecialDecay<Args>...> gears::functional::curry ( Function &&  f,
Args &&...  args 
)

Curries functions together. More info on what currying is can be found here.

Example:

#include <gears/functional.hpp>
#include <iostream>
namespace fn = gears::functional;
int main() {
auto add_five = fn::curry(fn::plus, 5);
std::cout << add_five(10) << ' ' << add_five(15);
}

Output

15 20
Parameters
fFirst function to curry
argsRest of the functions to curry
Returns
a function object that calls the curried functions

Definition at line 116 of file curry.hpp.

template<typename... Args>
template< typename...Args > auto gears::functional::invoke ( Args &&...  t)
noexcept

The INVOKE facility in the standard is specified in §20.8.2 as follows:

Define INVOKE (f, t1, t2, ..., tN) as follows:
— (t1.*f)(t2, ..., tN) when f is a pointer to a member function
of a class T and t1 is an object of type T or a reference to an object
of type T or a reference to an object of a type derived from T;
— ((*t1).*f)(t2, ..., tN) when f is a pointer to a member function
of a class T and t1 is not one of the types described in the previous item;
— t1.*f when N == 1 and f is a pointer to member data of a class T and
t1 is an object of type T or a reference to an object of type T or a reference
to an object of a type derived from T;
— (*t1).*f when N == 1 and f is a pointer to member data of a class
 T and t1 is not one of the types described in the previous item;
— f(t1, t2, ..., tN) in all other cases.
Parameters
targuments to pass to INVOKE facility.
Returns
generic result of the invoked call.

Definition at line 85 of file invoke.hpp.