这个元组创建成语是否有名称? [英] Is there a name for this tuple-creation idiom?

查看:336
本文介绍了这个元组创建成语是否有名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Boost邮寄名单 中,以下聪明的创建一个元组样实体的诀窍最近由@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作为输入。类似地, length 是一个lambda类型的列表实体,它将提供变量 sizeof ... 操作符到列表的原始输入参数。 sizeof ... 运算符被包装在lambda中,以便可以传递到列表



问题:这个元组创建成语是否有名称?

解决方案

我认为这是一个微妙的实现



Monads是一个用于模拟计算的不同步骤之间的状态的函数式编程结构(请记住,函数语言是无状态的)。

monad做的是链接不同的函数,创建一个计算管道,其中每个步骤都知道计算的当前状态。 / p>

Monads有两个主要区域:




  • 返回函数,

  • 一个绑定函数,它接受一个Monad-ready值(从上一个流水线步骤),并将其解包到其原始值,以传递值到下一步。



维基百科有非常好的例子和关于monads的解释。



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

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

我认为这里我们识别 return 的monad:取值并以Monadic方式返回。
具体来说,这个返回返回一个从tuple类别到可变包类别的函数(在数学意义上,不是一个C ++函子)。

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

pack_size

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

并且 length 版本的monad bind 运算符,这个运算符从上一个流水线步骤获取一个monadic值,并绕过它到指定的函数。 )。



最后,您的呼叫可以重写为:

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

因此, 这个元组创建成语的名字是什么? 好吧,我认为这可以称为单子类元组,因为它不完全是一个monad,但元组表示和扩展工作方式类似,保留到Haskell的延续monad。



编辑:更有趣



只是为了搞笑C ++编程,单子样的东西。您可以在此处找到一些示例。


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    
}

Live Example.

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.

解决方案

I think this is a subtle implementation of a Monad-like thing, specifically something in the same spirit of the continuation 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 have two primary pilars:

  • A return function, which takes a value and returns it in a Monad-ready form.
  • A bind function, which takes a Monad-ready value (From the previous pipeline step) and unwraps it to its original from to pass the value to the next step.

The Wikipedia has very good examples and explanations about monads.

Let me rewrite the given C++14 code:

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

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

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

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.

Edit: More fun

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

这篇关于这个元组创建成语是否有名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆