akasakaR's blog

By akasakaR, history, 3 months ago, In English

What is a Y combinator where I see some ppl using it, it looks something like this:

template<class F>
struct y_combinate_t {
  F f;
  template<class...Args>
  decltype(auto) operator()(Args&&...args)const {
    return f(*this, std::forward<Args>(args)...);
  }
};
template<class F>
y_combinate_t<std::decay_t<F>> y_combinate( F&& f ) {
  return {std::forward<F>(f)};
};
  • Vote: I like it
  • +18
  • Vote: I do not like it

»
3 months ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

It's a workaround for recursive lambdas (see here). I personally use std::function which can be called within itself.

  • »
    »
    3 months ago, # ^ |
    Rev. 3   Vote: I like it +16 Vote: I do not like it

    Using std::function runs the risk of TLEing, for most competitive programming purposes it is way better to use self-referential lambdas or y_combinator (which is a wrapper for self-referential lambdas, though sometimes optimizations don't see through this wrapper).

    C++23 has a feature called deducing this which is a pretty good replacement for these gymnastics (in fact, it was meant to do other things but it having this property is a nice side effect).

    • »
      »
      »
      3 months ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      Interesting, although I have not experienced TLEs in the past.

»
3 months ago, # |
  Vote: I like it +26 Vote: I do not like it

aryanc403 wrote a blog about it , you can find it here.

»
3 months ago, # |
  Vote: I like it +13 Vote: I do not like it