<bdo id='rM91J'></bdo><ul id='rM91J'></ul>
        <tfoot id='rM91J'></tfoot>
        <legend id='rM91J'><style id='rM91J'><dir id='rM91J'><q id='rM91J'></q></dir></style></legend>
        <i id='rM91J'><tr id='rM91J'><dt id='rM91J'><q id='rM91J'><span id='rM91J'><b id='rM91J'><form id='rM91J'><ins id='rM91J'></ins><ul id='rM91J'></ul><sub id='rM91J'></sub></form><legend id='rM91J'></legend><bdo id='rM91J'><pre id='rM91J'><center id='rM91J'></center></pre></bdo></b><th id='rM91J'></th></span></q></dt></tr></i><div id='rM91J'><tfoot id='rM91J'></tfoot><dl id='rM91J'><fieldset id='rM91J'></fieldset></dl></div>

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

        这个元组创建习惯用法有名字吗?

        时间:2023-06-30

            <i id='ONuLn'><tr id='ONuLn'><dt id='ONuLn'><q id='ONuLn'><span id='ONuLn'><b id='ONuLn'><form id='ONuLn'><ins id='ONuLn'></ins><ul id='ONuLn'></ul><sub id='ONuLn'></sub></form><legend id='ONuLn'></legend><bdo id='ONuLn'><pre id='ONuLn'><center id='ONuLn'></center></pre></bdo></b><th id='ONuLn'></th></span></q></dt></tr></i><div id='ONuLn'><tfoot id='ONuLn'></tfoot><dl id='ONuLn'><fieldset id='ONuLn'></fieldset></dl></div>
            <tfoot id='ONuLn'></tfoot>
              <tbody id='ONuLn'></tbody>
          1. <legend id='ONuLn'><style id='ONuLn'><dir id='ONuLn'><q id='ONuLn'></q></dir></style></legend>
          2. <small id='ONuLn'></small><noframes id='ONuLn'>

              • <bdo id='ONuLn'></bdo><ul id='ONuLn'></ul>
                  本文介绍了这个元组创建习惯用法有名字吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  Boost邮件列表上,以下聪明的@LouisDionne 最近发布了创建类似元组的实体的技巧:

                  On the Boost mailinglist, the following clever trick to create a tuple-like entity was recently posted by @LouisDionne:

                  #include <iostream>
                  
                  auto list = [](auto ...xs) { 
                      return [=](auto access) { return access(xs...); }; 
                  }; 
                  
                  auto length = [](auto xs) { 
                      return xs([](auto ...z) { return sizeof...(z); }); 
                  };
                  
                  int main()
                  {
                      std::cout << length(list(1, '2', "3")); // 3    
                  }
                  

                  实例.

                  聪明之处在于 list 是一个将可变参数列表作为输入的 lambda,并返回一个 lambda 作为输出,该 lambda 将采用另一个 lambda 对其输入进行操作.类似地,length 是一个采用类似列表实体的 lambda,它将向列表的原始输入参数提供可变参数 sizeof... 运算符.sizeof... 运算符被包裹在 lambda 中,以便它可以传递到 list.

                  The cleverness is that list is a lambda taking a variadic parameter-list as input, and returning a lambda as an output that will take another lambda to act on its input. Similarly, length is a lambda taking a list-like entity, to which it will supply the variadic sizeof... operator to the list's original input parameters. The sizeof... operator is wrapped inside a lambda so that it can be passed to the list.

                  问题:这个创建元组的习语有名字吗?可能来自更常用高阶函数的函数式编程语言.

                  Question: is there a name for this tuple-creation idiom? Perhaps from a functional programming language where higher-order functions are more commonly used.

                  推荐答案

                  我认为这是一个类似于 Monad 的东西的微妙实现,特别是与延续 monad 相同的东西.

                  I think this is a subtle implementation of a Monad-like thing, specifically something in the same spirit of the continuation monad.

                  Monads 是一种函数式编程结构,用于模拟计算的不同步骤之间的状态(请记住,函数式语言是无状态的).
                  monad 的作用是链接不同的函数,创建一个计算管道",其中每一步都知道计算的当前状态.

                  Monads are a functional programming construction used to simulate state between different steps of a computation (Remember that a functional language is stateless).
                  What a monad does is to chain different functions, creating a "computation pipeline" where each step knows about the current state of the computation.

                  Monads 有两个主要支柱:

                  Monads have two primary pilars:

                  • 一个返回函数,它接受一个值并以 Monad-ready 的形式返回它.
                  • 一个绑定函数,它接受一个 Monad-ready 值(来自上一个管道步骤)并将其解包到原始 from 以将该值传递给下一步.

                  维基百科有关于 monad 的很好的例子和解释.

                  The Wikipedia has very good examples and explanations about monads.

                  让我重写给定的 C++14 代码:

                  Let me rewrite the given C++14 code:

                  auto list = []( auto... xs ) 
                  { 
                      return [=]( auto access ) { return access(xs...); };
                  };
                  

                  我认为这里我们确定了 monad 的 return 函数:获取值并以 Monadic 方式返回它.具体来说,此返回返回一个从元组"类别到可变参数包类别的函子(在数学意义上,不是 C++ 函子).

                  I think here we identify the return function of a monad: Takes the value and returns it in a Monadic way. Specifically, this return returns a functor (In the mathematical sense, not a C++ functor) which goes from the "tuple" category to the variadic pack category.

                  auto pack_size = [](auto... xs ) { return sizeof...(xs); };
                  

                  pack_size 只是一个普通的函数.它将用于管道中以完成一些工作.

                  pack_size is just a normal function. It would be used in a pipeline to do some work.

                  auto bind = []( auto xs , auto op ) 
                  {
                      return xs(op);
                  };
                  

                  而且 length 只是一个接近于 monad bind 操作符的非通用版本,该操作符从前一个管道步骤中获取一个 monadic 值,并绕过到指定的函数(真正起作用的函数).该函数是此计算步骤完成的功能.

                  And length is only a non-generic version of something near to the monad bind operator, an operator which takes a monadic value from a previous pipeline step, and bypasses it to the specified function (Function which really does the work). That function is the functionality done by this computation step.

                  最后你的电话可以改写为:

                  Finally your call could be rewritten as:

                  auto result = bind(list(1,'2',"3"), pack_size);
                  

                  那么,这个元组创建习惯用法的名称是什么?好吧,我认为这可以称为类单子元组",因为它不完全是一个 monad,但是元组的表示和扩展以类似的方式工作,仍然是 Haskell 的 continuation monad.

                  So, whats the name of this tuple creation idiom? Well, I think this could be called "monad-like tuples", since its not exactly a monad, but the tuple representation and expansion works in a similar way, remaining to the Haskell continuation monad.

                  只是为了有趣的 C++ 编程的震撼,我一直在探索这个类似 monad 的东西.您可以在此处找到一些示例.

                  Just for the shake of funny C++ programming, I have followed exploring this monad-like thing. You could find some examples here.

                  这篇关于这个元组创建习惯用法有名字吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何立即调用 C++ lambda? 下一篇:返回 lambda 表达式的函数

                  相关文章

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

                    • <bdo id='EHvgh'></bdo><ul id='EHvgh'></ul>
                  1. <tfoot id='EHvgh'></tfoot>
                  2. <i id='EHvgh'><tr id='EHvgh'><dt id='EHvgh'><q id='EHvgh'><span id='EHvgh'><b id='EHvgh'><form id='EHvgh'><ins id='EHvgh'></ins><ul id='EHvgh'></ul><sub id='EHvgh'></sub></form><legend id='EHvgh'></legend><bdo id='EHvgh'><pre id='EHvgh'><center id='EHvgh'></center></pre></bdo></b><th id='EHvgh'></th></span></q></dt></tr></i><div id='EHvgh'><tfoot id='EHvgh'></tfoot><dl id='EHvgh'><fieldset id='EHvgh'></fieldset></dl></div>

                    <legend id='EHvgh'><style id='EHvgh'><dir id='EHvgh'><q id='EHvgh'></q></dir></style></legend>