C++ implements a Y function that can write a recursive lambda

  • 2020-04-02 02:30:37
  • OfStack

I've been learning about C++11's variadic template argument, so I'm happy to finally be able to get away from copying a lot of code using the fpmacro template. The main function of this example USES lambda to write a recursive function of the Fibonacci sequence. Unlike before, with the help of the Y function, the lambda expression is able to see itself successfully and call it recursively. Of course, this still requires a regular C++ recursive implementation, not the towering Y Combinator lambda -calculus.


#include <functional>
#include <memory>
#include <iostream>
#include <string>

using namespace std;

template<typename TResult, typename ...TArgs>
class YBuilder
{

private:

  function<TResult(function<TResult(TArgs...)>, TArgs...)> partialLambda;

public:

  YBuilder(function<TResult(function<TResult(TArgs...)>, TArgs...)> _partialLambda)

    :partialLambda(_partialLambda)

  {

  }

  TResult operator()(TArgs ...args)const

  {

    return partialLambda(

      [this](TArgs ...args)

      {

        return this->operator()(args...);

      }, args...);

  }

};

template<typename TMethod>

struct PartialLambdaTypeRetriver

{

  typedef void FunctionType;

  typedef void LambdaType;

  typedef void YBuilderType;

};

template<typename TClass, typename TResult, typename ...TArgs>

struct PartialLambdaTypeRetriver<TResult(__thiscall TClass::*)(function<TResult(TArgs...)>, TArgs...)>
{

  typedef TResult FunctionType(TArgs...);

  typedef TResult LambdaType(function<TResult(TArgs...)>, TArgs...);

  typedef YBuilder<TResult, TArgs...> YBuilderType;

};


template<typename TClass, typename TResult, typename ...TArgs>

struct PartialLambdaTypeRetriver<TResult(__thiscall TClass::*)(function<TResult(TArgs...)>, TArgs...)const>

{

  typedef TResult FunctionType(TArgs...);

  typedef TResult LambdaType(function<TResult(TArgs...)>, TArgs...);

  typedef YBuilder<TResult, TArgs...> YBuilderType;

};

 

template<typename TLambda>

function<typename PartialLambdaTypeRetriver<decltype(&TLambda::operator())>::FunctionType> Y(TLambda partialLambda)

{

  return typename PartialLambdaTypeRetriver<decltype(&TLambda::operator())>::YBuilderType(partialLambda);

}

 

int _tmain(int argc, _TCHAR* argv[])

{

  auto fib = Y([](function<int(int)> self, int index)

  {

    return index<2

      ?1

      :self(index-1)+self(index-2);

  });

 

  for (int i = 0; i < 10; i++)
  {

    cout << fib(i) << " ";

  }

  cout << endl;

}


Related articles: