什么是这个C ++ 14结构调用似乎链lambdas? [英] What is this C++14 construct called which seems to chain lambdas?
问题描述
这是关于此问题的后续问题: Lambda-Over-Lambda in C + +14 ,其中的答案解释了代码。
它是关于一个lambda,创建另一个lambda,当被调用时,调用传递的lambda并传递返回
这个例子显示了如何以这种方式链接lambdas。
从原始问题复制:
#include< cstdio>
auto terminal = [](auto term)//< --------- +
{// |
return [=](auto func)// | ???
{// |
return terminal(func(term)); //> --------- +
};
};
auto main() - > int
{
auto hello = [](auto s){fprintf(s,Hello\\\
);返回s; };
auto world = [](auto s){fprintf(s,World \\\
);返回s; };
terminal(stdout)
(hello)
(world);
return 0;
}
如果不是应该叫什么?
它类似于其他语言的结构吗?
注意:我不感兴趣它是否真的有用。
我看了一下,主要的功能是重新排序函数调用,如对原始问题的回答。
因此 world(hello(stdout)); 被重写为
terminal(stdout)(hello)(world);
我认为它只适用于体面的部分应用程序,lambdas提供了一点点,所以我们可以有 compose(4)([](int x){return x + 7;})([](int x){return x * 2;}) x){return x == 22;});
如果我的计算(和盲代码)是任何好的,它应该返回true。
或者强调部分应用:
auto add7 = [](int x){return x + 7; };
auto dbl = [](int x){return x * 2; };
auto equal22 = [](int x){return x == 22; };
assert(compose(4)(add7)(dbl)(equals22));
此实现的一个主要问题可能是无法评估结果,返回lambda,因此此答案中的构造可能更合适(函数以逗号而不是括号分隔)。
This is a follow-up question on this one: Lambda-Over-Lambda in C++14, where the answers explain the code.
It is about a lambda that creates another lambda which when called, calls the passed lambda and passes the return value to the original lambda, thus returning a new instance of the second lambda.
The example shows how this way lambdas can be chained.
Copy from the original question:
#include <cstdio>
auto terminal = [](auto term) // <---------+
{ // |
return [=] (auto func) // | ???
{ // |
return terminal(func(term)); // >---------+
};
};
auto main() -> int
{
auto hello =[](auto s){ fprintf(s,"Hello\n"); return s; };
auto world =[](auto s){ fprintf(s,"World\n"); return s; };
terminal(stdout)
(hello)
(world) ;
return 0;
}
Is there already a name for this construct and if not what should it be called? Does it resemble constructs in other languages?
Remark: I'm not interested in whether it is actually useful.
I looked around a bit and turns out the main functionality is reordering the function calls as explained in the answers to the original question.
So world(hello(stdout));
is rewritten to terminal(stdout)(hello)(world);
which more generally could be written as compose(stdout)(hello)(world);
.
- In Haskell this would written as
world . hello $ stdout
and is called function composition. - In clojure it would be (-> stdout hello world) and is called the "thread-first" macro
I think it is only useful with decent partial application which lambdas provide a little bit, so we could have compose(4)([](int x){ return x + 7; })([](int x){ return x * 2; })([](int x){ return x == 22; });
which should return true if my calculation (and blind coding) is any good.
or to emphasize the partial application:
auto add7 = [](int x){ return x + 7; };
auto dbl = [](int x){ return x * 2; };
auto equal22 = [](int x){ return x == 22; };
assert(compose(4)(add7)(dbl)(equals22));
1 major issue with this implementation is probably that the result can't be evaluated because in the end a lambda is returned, so the construction in this answer might be better suited (function separated by comma instead of parenthesis).
这篇关于什么是这个C ++ 14结构调用似乎链lambdas?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!