132: Data Types: Lambdas.

Take Up Code - A podcast by Take Up Code: build your own computer games, apps, and robotics with podcasts and live classes

Categories:

Lambdas are a recent addition to C++ and are also included in other languages. Think of them like unnamed methods but with some extra concepts that you need to know. Or if not, you’ll be even more lost than with function pointers. The C++ language has three ways to call code and other languages will be similar. What we’re mainly talking about here is not executing a method directly but instead passing a method to some other code that will run the method for you at some time either right away or in the future. The first is to just call a method directly. Method names are pointers and you can pass them to other methods to be called for you. This way also includes using function pointers. The second is to write a function object. This is a class that implements the function call operator. You can pass an instance of a function object class to another method to be called for you. And the third is to use a lambda expression which will create a temporary unnamed function object behind the scenes. This unnamed function object is called a closure. All lambdas start out with a lambda capture block that consists of a pair of square brackets. Inside these square brackets, you can list individual variables that the lambda will have access to. These variables get captured by the unnamed function object closure. That’s just a lambda way of saying that the function object will get initialized with either copies or references to the variables. Listen to the full episode for more on lambdas, or you can also read the full transcript below. Transcript I’m including lambdas under data types even though they’re not really a data type themselves. They’re actually expressions and have an unspecified type. But before all that, why would you want to use an unnamed method? I mean, I’ve talked at length about the importance of crafting good descriptive names. Why now, would you consider methods without names? Well, many times, you just want some small piece of code that you can send to another class to be run and this may be the only place in your code that you’ll ever need to use this code. You probably won’t need to ever call the code yourself. Creating a full method seems like a lot of unnecessary work. You have alternatives. The C++ language has three ways to call code and other languages will be similar. What we’re mainly talking about here is not executing a method directly but instead passing a method to some other code that will run the method for you at some time either right away or in the future. ◦ The first is to just call a method directly. Method names are pointers and you can pass them to other methods to be called for you. This way also includes using function pointers. ◦ The second is to write a function object. This is a class that implements the function call operator. You can pass an instance of a function object class to another method to be called for you. ◦ And the third is to use a lambda expression which will create a temporary unnamed function object behind the scenes. This unnamed function object is called a closure. Like I said, lambda expressions are a fairly new addition to C++ and other languages too. They’re really just a quick and short way of providing a function object. Make sure to listen to episodes 128 and 129 for more information about function objects. Normally, lambda expressions are just called lambdas and when I say they’re a quick and short way of writing a function object, they’re really easy to write. And they’re small enough to fit right into the rest of your code. Sometimes, this can cause lambdas to get lost in your code because they are so small and blend in with the rest of the code. This is just something to be aware of. I don’t think I’ve come across any code yet that I’d say tried to overuse lambdas. Probably the two most important aspects to understand are how to properly capture variables for us