Created
December 22, 2018 08:46
-
-
Save rileylev/048827c635636459c089b065df728609 to your computer and use it in GitHub Desktop.
Y Combinator in C++2a
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <utility> | |
template <class F> | |
struct Y{ | |
F f; | |
Y(F _f) : f{_f} {} | |
template<class...arg_t> | |
auto operator()(arg_t&&...arg) {return f(*this,std::forward<arg_t>(arg)...);} | |
}; | |
auto fact = Y{[](auto&& self, unsigned int n)->unsigned int { | |
if (n<=1) return 1; | |
return n*self(n-1)}}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment