Can someone explain why and when do we need to use Lambda in our code with some easy examples?

8/19/2018 12:34:40 PM

nAutAxH AhmAd

11 Answers

New Answer


Lambdas are anonymous functors used to declare functions on the go. You will probably need them when you require a predicate for specifying certain criteria in a function like sort, count_if, find_if, etc. Eg : std::vector<std::string> vec; // Add some elements to the vector. std::sort(vec.begin(),vec.end(), [](std::string a, std::string b){return a.size()<b.size();}); // Now std::sort will sort the vector based // on the size of the strings and arrange // them from smaller to larger. You can find more information here : https://stackoverflow.com/questions/7627098/what-is-a-lambda-expression-in-c11


"""this will be in python code why to use lambda? suppose u have define a function for eg; """ def my_func(f, arg): return f*5*arg """sup we dont want to change function name (here:my_func) because we have to use later in our code but we want to change argument of function in each time we code(here:f) so we can do with lambda """ my_func(lambda x: 2*x*x, 5)(2) """here we have taken f as a 2*x*x and here(2) is passes in side this lambda so it should be my_func(8, 5) and so in your mind que. arises that why dont we have put directly my_func(8, 5) . sup we dont wont to change 2*x*x this part but we have perform with different values that s why lambda is useful"""


Meet Rajpopat its alright bro


Meet Rajpopat can you explain in c++ because I don't know python yet


Mohamed ELomari I understood the code but then again why do we need to use Lambda since we can also do same thing like this : for(int i = 0; i < sizeof(array)/sizeof(array[0]); i++) array[i] *= 10; it will give the same output


the need for lambda came as a consequence of the wide adoption of the STL and its design ideas. the STL algorithms make frequent use of functors, without lambda, these functors need to be previously declared to be used. Lambdas make it possible to have 'anonymous', in-place functors.


instead of declaring the 2nd function you will eliminate extra lime of codes by using Lambda in a single line. def my_func(f, arg): return f(arg) print(my_func(lambda x: 2*x*x, 5)) def my_func1(f1, arg): return f1(arg) def f1(x): return 2*x*x print(my_func1(f1, 5)) both are out putting the same results, less writing with Lambda


here is an example using lambda to modify value of an array: https://code.sololearn.com/cNf1jUq2KNL8/#cpp std::transform ref: https://en.cppreference.com/w/cpp/algorithm/transform


let start from beginning...... i think u know how to define funcation. defining a fun comsumes time.. instead of defineing fun we can use lambda instead.... in which we can define funcation and its operation in a less amount of words


Lambdas are 'run as-is and forget'. This makes the code run faster and more efficient because functions and variables do not need to be referred to or saved. The concept of Lambda stumped me until I realised it's not really about saving typing, it's about slick code.


nAutAxH AhmAd sorry bro I can't help you with c++