Advertisement
Guest User

Untitled

a guest
Sep 27th, 2016
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.91 KB | None | 0 0
  1. // ConsoleApplication2.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <functional>
  6. #include <memory>
  7. #include <iostream>
  8. #include <string>
  9.  
  10. using namespace std;
  11.  
  12. template<typename TResult, typename ...TArgs>
  13. class YBuilder
  14. {
  15. private:
  16. function<TResult(function<TResult(TArgs...)>, TArgs...)> partialLambda;
  17.  
  18. public:
  19. YBuilder(function<TResult(function<TResult(TArgs...)>, TArgs...)> _partialLambda)
  20. :partialLambda(_partialLambda)
  21. {
  22. }
  23.  
  24. TResult operator()(TArgs ...args)const
  25. {
  26. return partialLambda(
  27. [this](TArgs ...args)
  28. {
  29. return this->operator()(args...);
  30. }, args...);
  31. }
  32. };
  33.  
  34. template<typename TMethod>
  35. struct PartialLambdaTypeRetriver
  36. {
  37. typedef void FunctionType;
  38. typedef void LambdaType;
  39. typedef void YBuilderType;
  40. };
  41.  
  42. template<typename TClass, typename TResult, typename ...TArgs>
  43. struct PartialLambdaTypeRetriver<TResult(__thiscall TClass::*)(function<TResult(TArgs...)>, TArgs...)>
  44. {
  45. typedef TResult FunctionType(TArgs...);
  46. typedef TResult LambdaType(function<TResult(TArgs...)>, TArgs...);
  47. typedef YBuilder<TResult, TArgs...> YBuilderType;
  48. };
  49.  
  50. template<typename TClass, typename TResult, typename ...TArgs>
  51. struct PartialLambdaTypeRetriver<TResult(__thiscall TClass::*)(function<TResult(TArgs...)>, TArgs...)const>
  52. {
  53. typedef TResult FunctionType(TArgs...);
  54. typedef TResult LambdaType(function<TResult(TArgs...)>, TArgs...);
  55. typedef YBuilder<TResult, TArgs...> YBuilderType;
  56. };
  57.  
  58. template<typename TLambda>
  59. function<typename PartialLambdaTypeRetriver<decltype(&TLambda::operator())>::FunctionType> Y(TLambda partialLambda)
  60. {
  61. return typename PartialLambdaTypeRetriver<decltype(&TLambda::operator())>::YBuilderType(partialLambda);
  62. }
  63.  
  64. int _tmain(int argc, _TCHAR* argv[])
  65. {
  66. function<int(int)> fib = Y([](function<int(int)> self, int index)
  67. {
  68. return index<2
  69. ?1
  70. :self(index-1)+self(index-2);
  71. });
  72.  
  73. for (int i = 0; i < 10; i++)
  74. {
  75. cout << fib(i) << " ";
  76. }
  77. cout << endl;
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement