Lumiera  0.pre.03
»edit your freedom«
function-closure.hpp File Reference

Go to the source code of this file.

Description

Partial function application and building a complete function closure.

This is a addendum to (and thin wrapper for) <functional>, supporting the case when a function should be closed over (partially or all) arguments, where especially the parameter values to close on are provided as a tuple. Additionally, we allow for composing (chaining) of two functions.

Because we have to deal with arbitrary functions and arbitrary parameter types, we need a lot of repetitive code to "catch" functions from zero to nine arguments. At the bottom of this header, you'll find a function-style interface, which wraps up all these technicalities.

Todo:

the implementation is able to handle partial application with N arguments, but currently we need just one argument, thus only this case was wrapped up into a convenient functions func::applyFirst and func::applyLast

11/23 these functor-utils were written at a time when support for handling generic functions in C++ was woefully inadequate; at that time, we neither had Lambda-support in the language, nor the ability to use variadic arguments. Providing a one-shot function-style interface for this kind of manipulations is still considered beneficial, and thus we should gradually modernise the tools we want to retain...

See also
control::CommandDef usage example
function.hpp

Definition in file function-closure.hpp.

#include "lib/meta/function.hpp"
#include "lib/meta/tuple-helper.hpp"
#include "lib/util.hpp"
#include <functional>
#include <utility>
#include <tuple>

Classes

struct  _Chain< FUN1, FUN2 >
 
struct  _Clo< SIG, ARG >
 
struct  _PapE< FUN >
 
struct  _PapS< FUN >
 
struct  _Sig< RET, ARG >
 
struct  Apply< n >
 this Helper with repetitive specialisations for up to nine arguments is used either to apply a function to arguments given as a tuple, or to create the actual closure (functor) over all function arguments. More...
 
struct  Apply< 0 >
 < Apply function without Arguments More...
 
struct  Apply< 1 >
 < Apply function with 1 Argument More...
 
struct  Apply< 2 >
 < Apply function with 2 Arguments More...
 
struct  Apply< 3 >
 < Apply function with 3 Arguments More...
 
struct  Apply< 4 >
 < Apply function with 4 Arguments More...
 
struct  Apply< 5 >
 < Apply function with 5 Arguments More...
 
struct  Apply< 6 >
 < Apply function with 6 Arguments More...
 
struct  Apply< 7 >
 < Apply function with 7 Arguments More...
 
struct  Apply< 8 >
 < Apply function with 8 Arguments More...
 
struct  Apply< 9 >
 < Apply function with 9 Arguments More...
 
class  BindToArgument< SIG, X, pos >
 Bind a specific argument to an arbitrary value. More...
 
class  FunctionClosure< SIG >
 Closing a function over its arguments. More...
 
struct  PartiallyInitTuple< SRC, TAR, start >::IndexMapper< idx, doPick >
 
struct  PartiallyInitTuple< SRC, TAR, start >::IndexMapper< idx, false >
 
class  PApply< SIG, VAL >
 Partial function application Takes a function and a value tuple, using the latter to close function arguments either from the front (left) or aligned to the end of the function argument list. More...
 
struct  PartiallyInitTuple< SRC, TAR, start >
 Builder for a tuple instance, where only some ctor parameters are supplied, while the remaining arguments will be default constructed. More...
 
struct  PlaceholderTuple< TYPES, i >
 Build a list of standard function argument placeholder types. More...
 
struct  PlaceholderTuple< Node< X, TAIL >, i >
 
struct  PlaceholderTuple< NullType, i >
 
class  TupleApplicator< SIG >
 Closure-creating template. More...
 

Functions

template<typename SIG , typename... ARG>
_Fun< SIG >::Ret apply (SIG &f, std::tuple< ARG... > &args)
 apply the given function to the argument tuple More...
 
template<typename FUN , typename ARG >
auto applyFirst (FUN &&fun, ARG &&arg)
 close the given function over the first argument. More...
 
template<typename FUN , typename ARG >
auto applyLast (FUN &&fun, ARG &&arg)
 close the given function over the last argument
 
template<typename SIG , typename TERM >
_PapE< SIG >::FunType::Functor bindLast (SIG &f, TERM const &arg)
 bind the last function argument to an arbitrary term, which especially might be a (nested) binder... More...
 
template<typename FUN1 , typename FUN2 >
auto chained (FUN1 &&f1, FUN2 &&f2)
 build a functor chaining the given functions: feed the result of f1 into f2. More...
 
template<typename SIG , typename... ARG>
_Clo< SIG, Types< ARG... > >::Type closure (SIG &f, std::tuple< ARG... > &args)
 close the given function over all arguments, using the values from the argument tuple. More...
 
template<typename... ARG>
_Sig< void, Types< ARG... > >::Applicator tupleApplicator (std::tuple< ARG... > &args)
 build a TupleApplicator, which embodies the given argument tuple and can be used to apply them to various functions repeatedly.
 

Namespaces

 lib
 Implementation namespace for support and library code.
 

Class Documentation

◆ lib::meta::func::anonymous_namespace{function-closure.hpp}::_Clo

struct lib::meta::func::anonymous_namespace{function-closure.hpp}::_Clo
Class Members
typedef _Fun< SIG >::Ret Ret
typedef _Sig< Ret, ARG >::Type Signature
typedef FunctionClosure
< Signature >
Type
+ Collaboration diagram for _Clo< SIG, ARG >:

◆ lib::meta::func::anonymous_namespace{function-closure.hpp}::_Sig

struct lib::meta::func::anonymous_namespace{function-closure.hpp}::_Sig
Class Members
typedef BuildFunType< RET, ARG >
::Sig
Type
typedef TupleApplicator< Type > Applicator
+ Collaboration diagram for _Sig< RET, ARG >:

◆ lib::meta::func::anonymous_namespace{function-closure.hpp}::Apply

struct lib::meta::func::anonymous_namespace{function-closure.hpp}::Apply
+ Collaboration diagram for Apply< n >:

◆ lib::meta::func::anonymous_namespace{function-closure.hpp}::PlaceholderTuple

struct lib::meta::func::anonymous_namespace{function-closure.hpp}::PlaceholderTuple
+ Collaboration diagram for PlaceholderTuple< TYPES, i >:

◆ lib::meta::func::anonymous_namespace{function-closure.hpp}::PlaceholderTuple< Node< X, TAIL >, i >

struct lib::meta::func::anonymous_namespace{function-closure.hpp}::PlaceholderTuple< Node< X, TAIL >, i >
Class Members
typedef typename
PlaceholderTuple< TAIL, i+1 >
::List
TailPlaceholders
typedef Node< _Placeholder< i >
, TailPlaceholders >
List
+ Collaboration diagram for PlaceholderTuple< Node< X, TAIL >, i >:

◆ lib::meta::func::anonymous_namespace{function-closure.hpp}::PlaceholderTuple< NullType, i >

struct lib::meta::func::anonymous_namespace{function-closure.hpp}::PlaceholderTuple< NullType, i >
Class Members
typedef NullType List
+ Collaboration diagram for PlaceholderTuple< NullType, i >:

Function Documentation

◆ apply()

_Fun<SIG>::Ret lib::meta::func::apply ( SIG &  f,
std::tuple< ARG... > &  args 
)
inline

apply the given function to the argument tuple

Deprecated:
11/23 meanwhile provided by the standard lib!

Definition at line 848 of file function-closure.hpp.

References lib::meta::func::apply().

Referenced by lib::meta::func::apply().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ closure()

_Clo<SIG,Types<ARG...> >::Type lib::meta::func::closure ( SIG &  f,
std::tuple< ARG... > &  args 
)
inline

close the given function over all arguments, using the values from the argument tuple.

Returns
a closure object, which can be invoked later to yield the function result.

Definition at line 863 of file function-closure.hpp.

References lib::meta::func::closure().

Referenced by lib::meta::func::closure().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ applyFirst()

auto lib::meta::func::applyFirst ( FUN &&  fun,
ARG &&  arg 
)
inline

close the given function over the first argument.

Warning
never tie an ownership-managing object by-value!

Definition at line 874 of file function-closure.hpp.

References lib::meta::func::applyFirst().

Referenced by lib::meta::func::applyFirst().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ bindLast()

_PapE<SIG>::FunType::Functor lib::meta::func::bindLast ( SIG &  f,
TERM const &  arg 
)
inline

bind the last function argument to an arbitrary term, which especially might be a (nested) binder...

Definition at line 899 of file function-closure.hpp.

References lib::meta::func::bindLast().

Referenced by lib::meta::func::bindLast().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ chained()

auto lib::meta::func::chained ( FUN1 &&  f1,
FUN2 &&  f2 
)
inline

build a functor chaining the given functions: feed the result of f1 into f2.

Note
the mathematical notation would be chained ≔ f2 ∘f1

Definition at line 914 of file function-closure.hpp.

References lib::meta::func::chained().

Referenced by lib::meta::func::chained().

+ Here is the call graph for this function:
+ Here is the caller graph for this function: