chore(cpp): add Y-combinator

Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
Matej Focko 2023-07-29 16:01:32 +02:00
parent 7c7377e138
commit b8e79c09f6
Signed by: mfocko
GPG key ID: 7C47D46246790496

View file

@ -93,6 +93,25 @@ std::vector<T> load_vector(std::size_t size) {
}
#pragma endregion /* input */
#pragma region functional
// http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0200r0.html
template <class Fun>
class y_combinator_result {
Fun fun_;
public:
template <class T>
explicit y_combinator_result(T &&fun) : fun_(std::forward<T>(fun)) {}
template <class... Args>
decltype(auto) operator()(Args &&...args) {
return fun_(std::ref(*this), std::forward<Args>(args)...);
}
};
template <class Fun>
decltype(auto) y_combinator(Fun &&fun) {
return y_combinator_result<std::decay_t<Fun>>(std::forward<Fun>(fun));
}
#pragma endregion /* functional */
#define LOOP(var, n) for (auto var = 0; var < n; ++var)