+ 11

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

Lambda

19th Aug 2018, 12:34 PM
blACk sh4d0w
blACk sh4d0w - avatar
10 Respostas
+ 6
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
19th Aug 2018, 1:37 PM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 5
"""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"""
19th Aug 2018, 1:07 PM
Meet Rajpopat
Meet Rajpopat - avatar
+ 5
Meet Rajpopat its alright bro
19th Aug 2018, 1:12 PM
blACk sh4d0w
blACk sh4d0w - avatar
+ 4
Meet Rajpopat can you explain in c++ because I don't know python yet
19th Aug 2018, 1:09 PM
blACk sh4d0w
blACk sh4d0w - avatar
+ 4
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
19th Aug 2018, 1:21 PM
blACk sh4d0w
blACk sh4d0w - avatar
+ 3
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.
19th Aug 2018, 1:30 PM
MO ELomari
MO ELomari - avatar
+ 3
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
22nd Aug 2018, 5:19 AM
cherif
+ 1
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
6th Sep 2018, 8:00 AM
lakshya mishra
lakshya mishra - avatar
+ 1
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.
21st Sep 2018, 6:48 AM
Arcayno
Arcayno - avatar
0
nAutAxH AhmAd sorry bro I can't help you with c++
19th Aug 2018, 1:11 PM
Meet Rajpopat
Meet Rajpopat - avatar