GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing functionalcpp.wordpress.com — Functional C++ We’ve seen how to use CPS and how to write new functions which combine other CPS functions. Combining functions by nesting lambdas, while neat, is pretty convoluted and an alternative way is sometimes preferred. Previously we had something to this effect: auto show = [](const auto& v){std::cout << v << "\n";};
auto one_cps = [](auto&& k){return k(1);};
auto double_cps = [](auto&& v, auto&& k){return k(v*2);};
And combining those to print auto two_cps = [&](auto&& k)
{
return one_cps([&](auto&& v){
return double_cps(v,k);
});
};
two_cps(show); // prints 2
The benefit of nesting lambdas is that you can capture the result of previous computations. In this case, there isn’t any need for that and we can use the alternative of writing continuations which return new CPS functions. For example, another way of writing the auto double_cont = [](auto&& v)
{
return [=](auto&& k){return k(v*2);};
};
The return value looks almost exactly like We could get an equivalent // prints 4 one_cps(double_cont)(double_cont)(show); A very important detail is that the return value of These special continuation functions are actually pretty versatile and can replicate the capturing that nested lambdas do as well. All we need is a continuation that returns a continuation which returns a CPS function! Let’s implement the Starting from the inside: [=](auto&& k){return k(lhs+rhs);};
So we have a CPS function that adds 2 things. Lets write a function that will return this, but with a captured variable, just as with [=](auto&& lhs){
return [=](auto&& k){return k(lhs+rhs);};
};
Turns out, adding more arguments is just capturing it within a lambda: auto add_cont_cont = [](auto&& rhs)
{
return [=](auto&& lhs){
return [=](auto&& k){return k(lhs+rhs);};
};
};
This can be used like this: // prints 3 one_cps(two_cps(add_cont_cont))(show); One last concept I want to demonstrate is a sort of “meta-continuation”. A function which takes a continuation and returns another continuation but modified in a certain way. For example this function will take a continuation, and return a continuation that applies itself twice into a CPS function: auto twice_meta = [](auto&& cont)
{
return [=](auto&& v)
{
return [=](auto&& k)
{
return cont(v)(cont)(k);
};
};
};
It’s return value is a typical continuation function. The innermost CPS function, however, calls the captured continuation twice before passing in the final continuation The final result of all of these functions is a set of easy to compose functions, albeit with a rather different syntax: // prints 10 one_cps(twice_meta(two_cps(add_cont_cont)))(double_cont)(show); I’ve tested on gcc-4.9, and it actually compiles down to the exact same as Personal statement: This post is well over a year late and for that I apologize. A lot has happened in that year but I hope to resume my postings as irregular as they may be. Thanks for reading! |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |