C++ 11 Lambda Functions

Поділитися
Вставка
  • Опубліковано 7 вер 2024

КОМЕНТАРІ • 145

  • @naganikhilbijjala1000
    @naganikhilbijjala1000 6 років тому +40

    Best video for Lambda functions. I wasted a lot of time with other articles and videos and finally came here. Your explanation is awsome.

    • @alltheway99
      @alltheway99 4 роки тому +1

      Agree, a good one,
      Another one I liked
      ua-cam.com/video/mWgmBBz0y8c/v-deo.html

    • @Manishbhaiya_rock
      @Manishbhaiya_rock 4 роки тому +1

      Agree

  • @benvaldivieso3326
    @benvaldivieso3326 Рік тому

    Thank you sir for posting the only lambda tutorial that makes any sense whatsoever, finally

  • @zijiezhou4179
    @zijiezhou4179 2 роки тому +3

    This is absolutely the clearest video for explaining the lambda function in C11! 😃

  • @danielgospodinow
    @danielgospodinow 7 років тому +64

    ""The return of the Lambda", haha sounds like a horror film." HAHAHA, love your tutorials!

  • @krantimadineni585
    @krantimadineni585 7 років тому +1

    OH! This is an amazing video ever made on c++11 Lambdas...All my doubts about Lambdas got resolved today. Thank You great man.

  • @communistgoatboy
    @communistgoatboy 8 років тому +34

    "i and g are read-only because we're passing by value to the body of our lambda."
    That's not _quite_ right. A lambda with a capture list is actually a functor whose `operator()` is const. When a variable is captured by value, it becomes a data member of the functor. Because const functions can't modify their data members (by default), then you can't modify variables captured by value. A reasonable person might then think that capturing by reference would capture by const reference. But no. Someone decided that capturing by reference would be done non-const. There is a way to allow const member functions to modify their data members: by marking them _mutable_. You can also do this with lambdas.
    int i = 3;
    auto f = [i]() mutable { i = 4; return i; };
    std::cout

    • @WhatsACreel
      @WhatsACreel  8 років тому +9

      +communistgoatboy Brilliant! Thanks for sharing.

  • @logomoniclearning6680
    @logomoniclearning6680 6 років тому

    best lamda function explanation on the tube yet

  • @shvideo1
    @shvideo1 5 років тому +1

    A great tutorial that builds gradually from the very basic to the more complex syntax, explaining all the aspects of lambda functions so clearly. Thank you!

  • @poganka45
    @poganka45 6 років тому +3

    best lambda explanation on the internet, thanks

  • @frozenfirebat
    @frozenfirebat 7 років тому +1

    Man, you just made my day... I've been trying to pass a complicated line of code into a std::thread for days now, and the Lambda function solved it.

  • @Kenforbes3
    @Kenforbes3 3 роки тому +2

    Best Lamda introduction I've found, very helpful. Thank you!

  • @Muhammad_Waleed
    @Muhammad_Waleed Рік тому

    "And You are all bunch of Legends" touched my Heart❤️
    Thank You Sir
    I have been trying to learn lambda functions from websites and didn't understand a single word
    A teacher is really Important Thanks a Lot again ❤️

  • @HiltonFernandes
    @HiltonFernandes 3 роки тому

    Amazing !
    Certainly the best introduction to C++ lambda functions, both in breadth of coverage and good humor.

  • @mohanrajanna9026
    @mohanrajanna9026 4 роки тому

    Understanding lamba is made very easy here. Thumps up for posting the video.

  • @djpeterson7479
    @djpeterson7479 8 років тому +20

    In your "simplest lambda", I think you can omit the parenthesis and make it even simpler:
    []{};
    (Works in Visual Studio at least, but I think this is standard).

  • @IceScream729
    @IceScream729 5 років тому +1

    amazing toturial finally somebody explains clear and with good examples!! thank you for the order man!

  • @vintage8818
    @vintage8818 7 років тому

    extremely informative tutorial video and extremely awesome way to deliver the lecture. Lambda functions suddenly seem so tameable to me now. Thanks. 😊

    • @WhatsACreel
      @WhatsACreel  7 років тому

      Welcome, thanks for watching!

  • @shrimpfights1714
    @shrimpfights1714 2 роки тому

    Clearest video ive watched on this thank you

  • @AegirAexx
    @AegirAexx 6 років тому

    Great video. You pushed me over the threshold. I'm finally understand lambdas. Thank you!

  • @s-c-iulian
    @s-c-iulian 6 років тому +1

    4:24 "The Return of The Lambda" , I just explode in laughter, the most epic things it can be heard on your channel ...

  • @majoro7251
    @majoro7251 8 років тому +2

    You're the good legend here Creel :D
    Awesome high-quality content, generally detailed low-level explanations, and cool Aussie accent. Gotta love this channel!
    Wish I could be a supportive patron, but can't from here. :/ Salutes from Syria anyways. :P
    (The hype of "Return of Lambda" is unprecedented). xD

    • @WhatsACreel
      @WhatsACreel  8 років тому +1

      +Major O Thanks, you're a hero!

    • @amanuel2135
      @amanuel2135 8 років тому

      +What's a Creel? No one who uses using namespace std; is a legend... Not only that but he does using namespace std, and then still just uses std::vector ...??

    • @creelsmusic5814
      @creelsmusic5814 8 років тому

      +Amanuel Bogale Classy! Can I get you some salsa to have with that chip on your shoulder?
      Seriously though, thanks for watching, and have a good one!

    • @amanuel2135
      @amanuel2135 8 років тому +1

      What's a Creel? i would actually love that. As One Of my Favorite(Eat it almost all the time) is Salsa!

  • @bouzie8000
    @bouzie8000 7 років тому +1

    This is a great video. You broke it down perfectly. Could you please do a series on systems programming

  • @alski259
    @alski259 Рік тому

    Outstanding simplicity and informative. Top notch!

  • @jjpark98
    @jjpark98 6 років тому

    Thanks, this really helped me understand. Wasn't as hard as I initially thought

  • @IllIlIIllIlIl
    @IllIlIIllIlIl 7 років тому +14

    12:10 This made me laugh harder than I care to admit..

    • @ChillerDragon
      @ChillerDragon 6 років тому

      xxxxxxxxxxxD true

    • @stevejones9044
      @stevejones9044 6 років тому

      I know right! He did that "asdfasfdasdfasfd" thing a few times which cracked me up every time.

    • @cloveramv
      @cloveramv 6 років тому +3

      And this Guys is what is *Ultra instincts !!*
      .
      .
      .
      15:17 *Ultra Instincts !!*

    • @adamodimattia
      @adamodimattia 4 роки тому

      yeah, it got me too, but when I rewatched it :) first time I was like: well that was a general example... :D

    • @Maduin1337
      @Maduin1337 2 роки тому

      Stroke Oriented Programming
      I laughed way too hard aswell, it's like getting your mate showing you some programming tricks. Laid back dude!

  • @sikendongol4208
    @sikendongol4208 5 років тому

    Return of the Lambda

  • @emirmujezinovic5550
    @emirmujezinovic5550 6 років тому

    Very nice video, you made it fun to learn it, thanks for it😁

  • @gprozac
    @gprozac 3 роки тому

    Thanks man,cool explanation about lambdas.

  • @davidutv
    @davidutv 8 років тому +3

    Hey mate, nicely done. I really enjoyed how you typed kjhgycikjhgfckiuy for explaining the definition of other code somewhere else :)
    One nit I did find, when you said end() provides a pointer to the (last) element; this isn't quite true :) I'm sure you know that end() returns one past the last element in the vector ;-)
    Still, great video; enjoyed it immensely!
    Happy New Year, BTW!

  • @quentinz6291
    @quentinz6291 3 роки тому

    Good tutorial for cpp lambda, thank you

  • @RaviKumar-zp8pm
    @RaviKumar-zp8pm 6 років тому

    love the way of teaching and some interesting dialogue in the middle made me laugh hahaha!!

  • @user-tv9mg4nt1q
    @user-tv9mg4nt1q 6 років тому

    It's very useful and easy to understand.

  • @mandrohz5598
    @mandrohz5598 8 років тому

    your tutorial is so good. clear fast and direct. really helpfull thanks!

  • @carlinconnell7715
    @carlinconnell7715 8 років тому +2

    Yeah, thanks man. Very good videos.

  • @muhammadhaseeb4096
    @muhammadhaseeb4096 6 років тому

    Thank you for this video.

  • @taitai5108
    @taitai5108 2 роки тому

    very detail explanation. thank you

  • @KunalMukherjee3701
    @KunalMukherjee3701 6 років тому

    Very nicely put, kudos

  • @PRATHAMPAI
    @PRATHAMPAI 6 років тому

    Amazing video, thank you so much.

  • @gnarfgnarf4004
    @gnarfgnarf4004 3 роки тому +2

    Thank you for a clear and lucid introduction to lambdas. However, I still don't see the benefit of lambdas. The additional complexity is not offset by the convenience. In fact, defining the function in-line complicates the flow of logic. Defining the lambda as a separate function, the old-fashion way, would improve code legibility.

  • @Wanderlust073
    @Wanderlust073 3 роки тому

    Excellent video

  • @WatchesTrainsAndRockets
    @WatchesTrainsAndRockets 3 роки тому

    The expression "using namespace std" is an abomination created in the textbooks and should be avoided. The problem is, for those who may not know, the using expression makes ALL of the symbols within the referenced namespace local to the context enclosing the using clause. If you happen to do this for multiple namespaces, you can get some weird collisions that are hard to diagnose.
    This possibility is eliminated by just not employing the using clause and explicitly specifying the namespace containing the symbol being referenced. In the code here that would mean writing "std::cout

  • @PhaseFabrication
    @PhaseFabrication 4 роки тому

    dude i love your videos

  • @bestraub98
    @bestraub98 8 років тому +1

    If you started your program without debugger (ctrl + F5), it would take a lot less time to start up. Also you could omit the breakpoint at the end, because the console stays open after that until you press enter. (at least in visual studio 2015)

    • @WhatsACreel
      @WhatsACreel  8 років тому

      +bestraub98 Cheers! Thanks for the info.

  • @TheGnarTube
    @TheGnarTube 6 років тому

    iterator end() points to one element past the end, not to the last element

  • @xpfe5zrm
    @xpfe5zrm 5 років тому

    Perfect, easy explanation. Thank you.

  • @bluehornet6752
    @bluehornet6752 5 років тому

    I think this shit is what Bjarne Stroustrup meant when he said "With C++ you can blow your whole leg off."

  • @oraclematon9277
    @oraclematon9277 3 роки тому

    great tutorial

  • @adamodimattia
    @adamodimattia 4 роки тому

    Good tutorial!

  • @novictim
    @novictim 3 роки тому

    Very informative !!!

  • @smeagolfishy
    @smeagolfishy 7 років тому

    Thank you! You're a great teacher

  • @privateerburrows
    @privateerburrows 3 роки тому

    Can you write lambdas that evaluate at compile time or static time, such as for initializing constants and static globals?
    There's a wonderful thing in the Eiffel language called "once function", which is any function pre-attributed with the keyword 'once', that only compute their output the first time they are called, but on subsequent calls return the same value they originally computed. They are a great replacement for global constants. In C-ish notation,
    once size_t bus_cycles_per_cache_line(){ cache_line_sz() / bus_width(); }
    //in some other file...
    once size_t cache_line_sz(){ cpuid(...); }
    once size_t bus_width(){ whatever(...); }
    It forces these evaluations to happen in the right order, as opposed to C/C++ which clearly states that the order of evaluation of static initializations is undefined.
    So these are lazy static initializations, at run-time for the most part, really; but they may be called as early as at compile time if a result is needed at compile time. I've often wondered if there's a way to replicate once functions in C++.

  • @defaultdefault812
    @defaultdefault812 3 роки тому

    How does foreach 'know' what to pass to the lambda, in this case int x. Expressed alternatively, how is int x being assigned?

  • @mahdies9620
    @mahdies9620 3 роки тому

    you are the best

  • @arbadon
    @arbadon 8 років тому

    Excellent explanation, thank you!

  • @jony7779
    @jony7779 7 років тому +1

    Thank you for this.

  • @manfredpseudowengorz
    @manfredpseudowengorz 3 роки тому

    2:45
    - Uuuuuu... What does this lambda do?
    - It compiles.

  • @mohshafi3032
    @mohshafi3032 6 років тому

    Thank you for your time, and a well explained lecture. Could you please explain in more detail why the ampersand(&) in front of the variable point (point&) in the sort algorithm by the lambda fragment of the code like this [ ](const point& a, const point& b). And also the purpose of the const.

    • @gnarfgnarf4004
      @gnarfgnarf4004 3 роки тому

      '&' pass by reference, instead of by value. Reduces the size of the stack if it's an object.
      'const': the function can't change the parameter.

  • @shashi3146
    @shashi3146 3 роки тому

    here in this lambda function we are passing [] (int x) what is x and how internally it is working.please elaborate it as i am new to this.

  • @filipjacobsen2468
    @filipjacobsen2468 6 років тому

    Thank you Obi One Kenobi :D

  • @jessprogany4345
    @jessprogany4345 3 роки тому

    loving your tutorials
    hope to see more of C++
    I have a question though
    does lamda function capture only the variables declared over lamda function definition or does it capture all of the variables of the scope where lamda function is defined?

    • @ME0WMERE
      @ME0WMERE Рік тому

      Probably only over, as otherwise you would be referring to a variable that doesn't exist

  • @rafaelocariz1384
    @rafaelocariz1384 5 років тому

    Hello! I'm facing some problems with lambda, I need to declare a function who execute something different depending on the input, on the other programs of the project this function is being called like this: int_op["add"](3, 5), but Lamba isn't supposed to be named and they are calling that way, how can I implement this int_op function?? please, help

  • @duydong78
    @duydong78 7 років тому

    Great explanation! Thanks!

  • @RupertBruce
    @RupertBruce Рік тому

    A new tool for obfuscation! It looks to be a way of doing functional programming in C++ but it has a lot of potential for abuse!

  • @fipabrate
    @fipabrate 6 років тому

    In last example ( 18:22 ) what is the difference between (function f ) and (function &f ). Basically, when should i use '&f' and when just 'f'

    • @asiseverything3404
      @asiseverything3404 3 роки тому

      &f and f can be used interchangeably. typing &f is a good practice though.

  • @himansumaurya2883
    @himansumaurya2883 3 роки тому

    Can u make video on recursive lamda using y_combinator

  • @narayananms620
    @narayananms620 8 років тому

    brillaint guy/video, loved it

  • @abhikghosh5220
    @abhikghosh5220 Рік тому

    The best

  • @JL-uz1fu
    @JL-uz1fu 8 років тому +1

    When I tried running the first program, it just says:
    q.cpp:10:7: error: ‘sayHelloWorld’ does not name a type

  • @BryonLape
    @BryonLape 7 років тому

    Why is it that C++ seems to break encapsulation at every opportunity?

  • @tuckerhartland1001
    @tuckerhartland1001 2 роки тому

    I don't really understand why you say that the lambda function has no name. In the first example isn't "sayHelloWorld" acting like a name to the lambda?

  • @bandamkaromi
    @bandamkaromi 8 років тому +1

    thanks..

  • @akash-iichaudhari5123
    @akash-iichaudhari5123 4 роки тому

    Day saver

  • @Asimov16
    @Asimov16 7 років тому

    Can you tell me what use is Lambda's when you could easily just make a function or method instead. I understand how they are used now thanks, just can't understand their use if you can use a function

    • @WhatsACreel
      @WhatsACreel  7 років тому

      They're just a different way to do things. They don't offer anything new, but they're pretty convinient soemtimes. I think the best examples of how powerful they are is when you look at processing lists, like filtering of perfomring some operation on the values.
      I hoep that makes some sense...?

  • @spread2081
    @spread2081 4 роки тому

    wait this is cool

  • @Rokannon
    @Rokannon 8 років тому

    This video is really good. Thank you very much. The only thing that I would like to have to know after this video is whether anonymous functions have any overhead comparing to classic approach. Because C++ is a great language performance-wise and using these fancy tricks by paying some performance cost would be just gay.
    Also I have subscribed to your channel. Many interesting videos on C++ I see. Because I am studying C++ at the moment and when you study new programming language it is really important to see someone having fun with it which you totally do, sir. But more importantly I am fading away from such politicaly incorrect languages as Java and ActionScript. If you heard some stories about garbage collection and virtual machine performance issues then you know what I mean.

  • @ADonT100
    @ADonT100 8 років тому

    can you pls explain the Operator Overloading ,Element & globale Funktion

  • @malharjajoo7393
    @malharjajoo7393 7 років тому

    The syntax in your first few slides does not have the "=" sign after the name. Are they incorrect ?

  • @t-h787
    @t-h787 6 років тому

    epic video mate, very informative + i lol'd several times :P

  • @aioaneiadrian9232
    @aioaneiadrian9232 7 років тому

    Nice job :)

  • @commentor5479
    @commentor5479 3 роки тому

    So you're that youtuber the other channel was asking about!

    • @WhatsACreel
      @WhatsACreel  3 роки тому

      Not sure mate, what channel?
      Thanks for watching :)

    • @commentor5479
      @commentor5479 3 роки тому

      @@WhatsACreel The channel named "What's a Creel?"

    • @WhatsACreel
      @WhatsACreel  3 роки тому

      @@commentor5479 Hahaha, that's gold! I fell for that hook line and sinker :)

    • @commentor5479
      @commentor5479 3 роки тому

      Wait. You're the same channel.

    • @WhatsACreel
      @WhatsACreel  3 роки тому

      @@commentor5479 Yep, just changed the name :)

  • @sikendongol4208
    @sikendongol4208 5 років тому

    in place

  • @pengzuo7551
    @pengzuo7551 7 років тому

    impressed , by the way, which accent is this speaker.

    • @WhatsACreel
      @WhatsACreel  7 років тому

      Thanks for watching. I'm Australian :)

  • @schermann1995
    @schermann1995 7 років тому

    very helpful thx :)

  • @ExD3aD
    @ExD3aD 6 років тому

    shouldn't the int [&](int x) be [&](int& arr) 12:52 ?

  • @malharjajoo7393
    @malharjajoo7393 7 років тому

    what is the difference between capure list ( "[=]" ) and passing by value in paramter list. ?

    • @nikarm22
      @nikarm22 7 років тому

      Capture list takes values when lambda is defined, but parameter is given when you are calling the function.
      So when you call the same lambda, you can pass different params, but capture list is always the same.

  • @yousufazad6914
    @yousufazad6914 8 років тому +1

    make another video please! :D

  • @hatedbytheworld1373
    @hatedbytheworld1373 5 років тому

    Easy as Pi😄

  • @cloveramv
    @cloveramv 6 років тому

    15:17 *Ultra Instincts !!*

  • @Sarcasticpigeon
    @Sarcasticpigeon 6 років тому

    Great video, but I cannot get over the lack of indentation. (0-e)

    • @WhatsACreel
      @WhatsACreel  6 років тому +1

      Hahaha, the indentation in this vid is a mess! I normally use Whitesmith. It's not popular, but it's super easy to read. But I have to save space on the slides, so I used like K&R. Then I mixed the two together because I'm a moron :S Sorry if it was confusing, hope the vid still made sense and thanks for watching!

    • @Sarcasticpigeon
      @Sarcasticpigeon 6 років тому

      What's a Creel? It was definitely helpful, I loved how quickly you move through it. :)

  • @abcdeereijgfhd3215
    @abcdeereijgfhd3215 2 роки тому +1

    I don't like the naming of std since it also means "Sexually Transmitted Desease"

  • @oddarnebeck
    @oddarnebeck 7 років тому

    I guess there's a reason for the guys saying "stood" instead of "ess tee dee" when talking about "std::".
    Good video though, thanks!

    • @azzam7239
      @azzam7239 3 роки тому

      I think because std usually stands for sexually transmitted disease

  • @Coffeemancer
    @Coffeemancer 2 роки тому

    can you tell C# ?

  • @nikolaossmyrnioudis397
    @nikolaossmyrnioudis397 6 років тому

    Eric Idle, is that you ?

    • @WhatsACreel
      @WhatsACreel  6 років тому

      Hahaha, being likened to a Python is the greatest honor of all!!

  • @srahimi5007
    @srahimi5007 7 років тому +1

    Lambda if not used properly produces more mess than good. I acknowledege its existance but wont use it . It is in contrarty to what the concept of OO seems to be.
    why would you wanna have a function within main? it is turning this language into something worse than assmembler.... however tutorial was excellent

    • @DuhBroadcaster
      @DuhBroadcaster 7 років тому +1

      Not every problem needs to be OO-style. In some cases, it's just a waste of time and work to try converting it into that style. Dismissing a tool can only hurt you as a programmer.

  • @kumu2024
    @kumu2024 4 роки тому

    It is just about making harder to read.. I don't think there is any performance benefit of this feature. Rewriting you old code and making it harder to read. That is all about .. in my opinion.
    Many new CPP technique is like so.. It is not really a new thing than making it harder to read.

  • @clifforddicarlo9178
    @clifforddicarlo9178 Рік тому

    I REALLY don't see or appreciate the value of lambda functions. All it does is make your code less readable. Can C++ "live"without Lambda Functions?

  • @pasanyrebiata5430
    @pasanyrebiata5430 6 років тому +2

    Do you like to drink?
    I think do you be nice alcoholism companion

  • @xDR1TeK
    @xDR1TeK 6 років тому

    I haven't coded in a while. I saw it in a coding conference and raised flags on me. I think it is an abomination. It infringes on many coding conventions. Conventions that were specifically agreed upon to prevent loosely written code that ends up churning useless asm lines. Mind you that this is only churning at compile time, but still, convention says changing one line or adding few years to this code, compile is no longer guaranteed. I think behavior wouldn't be guaranteed at all. What strikes me the most, people who are in position to make changes to compilers forget that there are people in this world that are not just software oriented but also hardware oriented. This is why I think people making changes to the compiler are not fit to make changes at all. I know being old fashioned has no place in the future but just saying you know. I'm old. I don't like this new change because lambdas are imposing suggestions that infringe on conventions.

    • @WhatsACreel
      @WhatsACreel  6 років тому +1

      You don't like lambdas?
      Yes, they are a little strange for C++. The syntax is probably my least favourite thing about them. But, they're really fun and fairly useful time savers sometimes, so it's ok to get used to them. Thanks for watching anywho, have a good one!

  • @SearchingPeaceInside
    @SearchingPeaceInside 7 років тому

    Good Tutorial.