What is this C++14 construct called which seems to chain lambdas?

本小妞迷上赌 提交于 2019-12-03 11:36:46
stefaanv

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

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

terminal(x) returns an applicator that method-chains its return value into terminal for repeated invocation.

But we could instead generalize it.

Suppose you have a function F. F takes an argument, and stuffs it on a stack.

It then examines the stack. If the top of the stack, evaluated on some subset of the stack, would work for invocation, it does it, and pushes the result back onto the stack. In general, such invocation could return a tuple of results.

So:

F(3)(2)(add)(2)(subtract)(7)(3)(multiply)(power)

would evaluate to:

((3+2)-2)^(7*3)

Your terminal does this with 0 argument functions (the first argument) and with 1 argument functions (every argument after that), and only supports 1 return value per invocation.

Doing this with a lambda would be tricky, but what I described is doable in C++.

So one name for it would be stack-based programming.

As far as I know there is no "official" name, yet.

Suggestions:

  • Lambda chain
  • Lambda sausage
  • Curry sausage
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!