久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

    <small id='W60wO'></small><noframes id='W60wO'>

        <bdo id='W60wO'></bdo><ul id='W60wO'></ul>
      <i id='W60wO'><tr id='W60wO'><dt id='W60wO'><q id='W60wO'><span id='W60wO'><b id='W60wO'><form id='W60wO'><ins id='W60wO'></ins><ul id='W60wO'></ul><sub id='W60wO'></sub></form><legend id='W60wO'></legend><bdo id='W60wO'><pre id='W60wO'><center id='W60wO'></center></pre></bdo></b><th id='W60wO'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='W60wO'><tfoot id='W60wO'></tfoot><dl id='W60wO'><fieldset id='W60wO'></fieldset></dl></div>
      <tfoot id='W60wO'></tfoot>
      1. <legend id='W60wO'><style id='W60wO'><dir id='W60wO'><q id='W60wO'></q></dir></style></legend>
      2. 通過泛型 lambda 理解 Y Combinator

        Understanding Y Combinator through generic lambdas(通過泛型 lambda 理解 Y Combinator)
        <tfoot id='zzNBU'></tfoot>

        1. <small id='zzNBU'></small><noframes id='zzNBU'>

              <tbody id='zzNBU'></tbody>
              <bdo id='zzNBU'></bdo><ul id='zzNBU'></ul>
              • <legend id='zzNBU'><style id='zzNBU'><dir id='zzNBU'><q id='zzNBU'></q></dir></style></legend>

                <i id='zzNBU'><tr id='zzNBU'><dt id='zzNBU'><q id='zzNBU'><span id='zzNBU'><b id='zzNBU'><form id='zzNBU'><ins id='zzNBU'></ins><ul id='zzNBU'></ul><sub id='zzNBU'></sub></form><legend id='zzNBU'></legend><bdo id='zzNBU'><pre id='zzNBU'><center id='zzNBU'></center></pre></bdo></b><th id='zzNBU'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='zzNBU'><tfoot id='zzNBU'></tfoot><dl id='zzNBU'><fieldset id='zzNBU'></fieldset></dl></div>
                1. 本文介紹了通過泛型 lambda 理解 Y Combinator的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  在構建一個基于 lambda 的小型元編程庫時,我有必要在 C++14 通用 lambda 中使用遞歸來實現(xiàn)一個 左折疊.

                  While building a small lambda-based metaprogramming library, I had the necessity of using recursion in a C++14 generic lambda, to implement a left-fold.

                  我自己的解決方案是將 lambda 本身作為其參數(shù)之一傳遞,如下所示:

                  My own solution was passing the lambda itself as one of its parameters, like this:

                  template <typename TAcc, typename TF, typename... Ts>
                  constexpr auto fold_l_impl(TAcc acc, TF f, Ts... xs)
                  {
                      // Folding step.
                      auto step([=](auto self)
                      {
                          return [=](auto y_acc, auto y_x, auto... y_xs)
                          {
                              // Compute next folding step.
                              auto next(f(y_acc, y_x));
                  
                              // Recurse if required.
                              return static_if(not_empty(y_xs...))
                                  .then([=]
                                      {
                                          // Recursive case.
                                          return self(self)(next, y_xs...);
                                      })
                                  .else_([=]
                                      {
                                          // Base case.
                                          return next;
                                      })();
                          };
                      });
                  
                      // Start the left-fold.
                      return step(step)(acc, xs...);
                  }
                  

                  step 是開始遞歸的主要"lambda.它返回一個具有所需左折疊簽名的函數(shù)(累加器,當前項目,剩余項目...).

                  step is the "main" lambda that starts off the recursion. It returns a function with the desired left-fold signature (accumulator, current item, remaining items...).

                  該函數(shù)使用self(self)(next, y_xs...)遞歸調用自身.

                  The function calls itself recursively by using self(self)(next, y_xs...).

                  我最近遇到了這個提議想在標準庫中加入一個Y Combinator,看了之后,和我在這里做的事情非常相似.

                  I've recently come across this proposal that wants to add a Y Combinator to the Standard Library, and after reading it, it seems extremely similar to what I am doing here.

                  不幸的是,Y Combinator 的概念對我來說仍然沒有點擊"——我遺漏了一些東西,我無法想象如何概括我對任何函數(shù)的 self 參數(shù)所做的事情,避免 step 樣板.

                  Unfortunately, the concept of the Y Combinator still doesn't "click" for me - I am missing something and I cannot visualize how to generalize what I did with the self parameter for any function, avoiding the step boilerplate.

                  我已經(jīng)閱讀了關于此事的這個優(yōu)秀的 StackOverflow 答案,但它仍然沒有'為我點擊".

                  I've read this excellent StackOverflow answer regarding the matter, but it still didn't "click" for me.

                  (來自那個答案) 遞歸階乘是這樣定義的:

                  (From that answer) a recursive factorial is defined this way:

                  fact =
                    (recurs) =>
                      (x) =>
                        x == 0 ? 1 : x * recurs(x - 1);
                  

                  recurs 參數(shù)似乎與我的 self 參數(shù)具有相同的作用.我不明白的是如何在不將 recurs 再次傳入自身的情況下調用 recurs.

                  The recurs parameter seems to have the same role as my self parameter. What I do not understand is how recurs is called without passing recurs into itself again.

                  我必須像這樣調用 self:self(self)(params...).

                  I have to call self like this: self(self)(params...).

                  recurs 的調用方式類似于 recurs(params...).

                  recurs, however, is called like recurs(params...).

                  嘗試調用 self(params...) 會導致編譯器錯誤,通知我 self 只需要一個參數(shù) (這是 auto self lambda 參數(shù)).

                  Attempting to call self(params...) results in a compiler error informing me that self requires only a single parameter (which is the auto self lambda parameter).

                  我在這里錯過了什么?我怎樣才能重寫我的 fold_l_impl lambda,使其遞歸可以通過使用 Y Combinator 來泛化?

                  What am I missing here? How could I rewrite my fold_l_impl lambda in such a way that its recursion could be generalized through the use of a Y Combinator?

                  推薦答案

                  這里是一個 y 組合,其中 lambda 被傳遞一個不需要傳遞的遞歸:

                  Here is a y combinate where the lambda is passed a recurs that doesn't need to be passed recurs:

                  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)};
                  };
                  

                  然后你做:

                    return y_combinate(step)(acc, xs...);
                  

                  和改變

                                     return self(self)(next, y_xs...);
                  

                                     return self(next, y_xs...);
                  

                  這里的技巧是我使用了一個非 lambda 函數(shù)對象,它可以訪問自己的 this,我將其作為第一個參數(shù)傳遞給 f.

                  the trick here is I used a non-lambda function object that has access to its own this, which I pass to f as its first parameter.

                  這篇關于通過泛型 lambda 理解 Y Combinator的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

                  【網(wǎng)站聲明】本站部分內容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯(lián)系我們刪除處理,感謝您的支持!

                  相關文檔推薦

                  In what ways do C++ exceptions slow down code when there are no exceptions thown?(當沒有異常時,C++ 異常會以何種方式減慢代碼速度?)
                  Why catch an exception as reference-to-const?(為什么要捕獲異常作為對 const 的引用?)
                  When and how should I use exception handling?(我應該何時以及如何使用異常處理?)
                  Scope of exception object in C++(C++中異常對象的范圍)
                  Catching exceptions from a constructor#39;s initializer list(從構造函數(shù)的初始化列表中捕獲異常)
                  Difference between C++03 throw() specifier C++11 noexcept(C++03 throw() 說明符 C++11 noexcept 之間的區(qū)別)
                  <tfoot id='6EIqu'></tfoot>
                    <bdo id='6EIqu'></bdo><ul id='6EIqu'></ul>

                        <tbody id='6EIqu'></tbody>

                      <small id='6EIqu'></small><noframes id='6EIqu'>

                        • <legend id='6EIqu'><style id='6EIqu'><dir id='6EIqu'><q id='6EIqu'></q></dir></style></legend>

                            <i id='6EIqu'><tr id='6EIqu'><dt id='6EIqu'><q id='6EIqu'><span id='6EIqu'><b id='6EIqu'><form id='6EIqu'><ins id='6EIqu'></ins><ul id='6EIqu'></ul><sub id='6EIqu'></sub></form><legend id='6EIqu'></legend><bdo id='6EIqu'><pre id='6EIqu'><center id='6EIqu'></center></pre></bdo></b><th id='6EIqu'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='6EIqu'><tfoot id='6EIqu'></tfoot><dl id='6EIqu'><fieldset id='6EIqu'></fieldset></dl></div>

                            主站蜘蛛池模板: 国产精品久久久久久久久久久免费看 | 亚洲国产精品一区二区三区 | 中文字幕精品视频在线观看 | 欧美日韩三级 | 大学生a级毛片免费视频 | 女人毛片a毛片久久人人 | 欧美日本韩国一区二区 | 亚洲日本免费 | 九九福利 | 国产精品久久久久久久免费大片 | 天堂av免费观看 | h视频免费在线观看 | h视频在线播放 | 欧美一区二区三区在线观看 | 一级做a毛片 | 成人亚洲一区 | 欧美一区二区免费视频 | 亚洲欧美中文字幕在线观看 | 国产成人久久精品一区二区三区 | 免费看国产片在线观看 | 中文字幕国产 | 中文字幕av一区二区三区 | 欧美性大战久久久久久久蜜臀 | 国产一级毛片视频 | 精品无码久久久久久久动漫 | 麻豆va| 欧美日韩在线免费 | 久久国产精品一区二区三区 | 一区二区三区在线免费观看 | 精品乱码一区二区三四区 | 中文字幕av中文字幕 | 日韩av成人 | 欧美一区二区三区视频在线播放 | 国产精品免费在线 | 亚洲精品成人网 | av日韩精品 | 欧美一区二区免费 | av永久 | 日韩久久久久 | 天天躁日日躁aaaa视频 | 国产97人人超碰caoprom |