2 ms·
I don't see how you want to use first-class functions, lambdas, in this situation. Maybe, you meant to say nested functions? Lambda function is a piece of code
by a-nikolaev 12y ago
I don't see how you want to use first-class functions, lambdas, in this situation. Maybe, you meant to say nested functions? Lambda function is a piece of code that is usually sent somewhere else, a callback basically.
If you define a lambda function instead of a block of code and call it right there, it would create a considerable overhead: because if it is a closure, it should save its current scope somewhere. And it is really unnecesary, if you call this function right at the same place where you define it.
- corysama 12y agosjolsen did a good job of illustrating what I meant https://news.ycombinator.com/item?id=8375341 https://news.ycombinator.com/item?id=8375341 Improvements on his version would be to make everything const and the lambda inputs explicit. AwesomenessT largerFunction(Foo1 foo1, Foo2 foo2) { const ResultT1 result1 = [foo1] { const Bar bar = barFromFoo1(foo1); const Baz baz = bar.makeBaz(); return baz.awesome(); } (); const ResultT2 result2 = [foo2] { const Bar bar = barFromFoo2(foo2); return bar.awesome(); } (); return result1.howAwesome(result2); } It's my understanding that compilers are already surprisingly good at optimizing out local lambdas. I recall a demo from Herb Sutter where std::for_each(someLambda) was faster than a classic for(int i;i<100000;i++) loop with a trivial body because the for_each internally unrolled the loop and the lamdba body was therefore inlined as unrolled.