jaulib v1.3.0
Jau Support Library (C++, Java, ..)
Classes | Public Types | Static Public Member Functions | List of all members
jau::func::ylambda_target_t< R, L, A > Class Template Referencefinal

func::ylambda_target_t is a Y combinator and deducing this implementation for lambda closures usable for recursive algorithms. More...

#include <functional.hpp>

Collaboration diagram for jau::func::ylambda_target_t< R, L, A >:

Public Types

typedef delegate_t< R, A... > delegate_type
 

Static Public Member Functions

static delegate_type delegate (L function) noexcept
 

Detailed Description

template<typename R, typename L, typename... A>
class jau::func::ylambda_target_t< R, L, A >

func::ylambda_target_t is a Y combinator and deducing this implementation for lambda closures usable for recursive algorithms.

The Y combinator allows passing the unnamed lambda instance itself, enabling recursive invocation from within the lambda.
In other words, a this reference of the unnamed lambda is passed to the lambda, similar to Deducing this.

The ylambda function<R(A...)> is invoked w/o explicitly passing the object parameter, as it is implicitly passed down to the user's lambda implementation.

Example implementing a recursive lambda factorial function

function<int(int)> f1 = function<int(int)>::bind_ylambda( [](auto& self, int x) -> int {
if( 0 == x ) {
return 1;
} else {
return x * self(x-1); // recursion, calling itself w/o explicitly passing `self`
}
} );
assert( 24 == f1(4) ); // `self` is bound to delegate<R(A...)> `f.target`, `x` is 4
// or using explicit function<R(A...)>::delegate_type
function<int(int)> f2 = function<int(int)>::bind_ylambda( [](function<int(int)>::delegate_type& self, int x) -> int {
if( 0 == x ) {
return 1;
} else {
return x * self(x-1); // recursion, calling itself w/o explicitly passing `self`
}
} );
assert( 24 == f2(4) ); // `self` is bound to function<int(int)>::delegate_type `f.target`, `x` is 4
delegate_t< R, A... > delegate_type
Class template jau::function is a general-purpose static-polymorphic function wrapper.

An instance is identifiable as func::target_type::ylambda via jau::function<R(A...)>::type().

Template Parameters
Rfunction return type
Ltypename holding the lambda closure
Afunction arguments
See also
Function Overview
Deducing this
Explicit object parameter
Curiously Recurring Template Pattern
Examples
test_functional.hpp.

Definition at line 1005 of file functional.hpp.

Member Typedef Documentation

◆ delegate_type

template<typename R , typename L , typename... A>
typedef delegate_t<R, A...> jau::func::ylambda_target_t< R, L, A >::delegate_type

Definition at line 1007 of file functional.hpp.

Member Function Documentation

◆ delegate()

template<typename R , typename L , typename... A>
static delegate_type jau::func::ylambda_target_t< R, L, A >::delegate ( function)
inlinestaticnoexcept

Definition at line 1041 of file functional.hpp.

Here is the caller graph for this function:

The documentation for this class was generated from the following file: