Namespaces
Variants
Views
Actions

Talk:cpp/language/lambda

From cppreference.com

I have some changes I'd like to make (or seen made) but I want to open it up for feedback first rather than possibly having the changes rolled back.

First, I'd like to start the description with something simpler, along the lines of "A Lambda Function is an anonymous (inline and unnamed) function called via function pointer or functor.

Next, I think the first part of the example might be too complicated, obscuring the role of the lambda part. I suggest that there is first a more basic loop with a traditional functor, then a similar task done via a lambda function.

The last part of the example, as it is, seems like it will be confusing to some as std::function is newer, and not obvious, the example might seem like a just a complicated way to code: int func(int i) {return i+4;}

Arbalest 09:11, 1 November 2012 (PDT)

I feel that such description could be misleading: it would be introducing the general programming concept 'lambda function', not the C++ 'lambda expression' or the C++ 'closure object'. It could be done (and probably should be done, eventually, seeing as the title of this page is 'lambda functions', which are never mentioned in the C++ standard or in the body of the page), but it would have to explain the difference first. The example currently is as basic as it gets: lambdas were made specifically to make algorithms easier to use (a loop would be neither simple nor typical), and std::function is the most common way to manipulate a closure. If anything, it could use a couple more algorithms, especially ones that make use of lambda captures. --Cubbi 10:43, 1 November 2012 (PDT)
I also don't see a lot of benefit to the description change, but I do think that we could start off with an even simpler example -- specifically, one that doesn't use the erase-remove idiom. How about we start with using std::count_if to count the number of numbers in the vector that match some predicate, and then continue with the rest of the current example? -Nate 11:42, 1 November 2012 (PDT)
By loop I mean for_each(begin, end, functor) followed by for_each(begin, end, lambda-exp). Arbalest 15:29, 1 November 2012 (PDT)
I think a std::for_each example would also work fine. If we use both a functor and a lambda-exp, it would be good to make sure that it's clear that the functor is just there for juxtaposition and isn't required to make the lambda work. -Nate 12:37, 2 November 2012 (PDT)