1 00:00:07,620 --> 00:00:13,590 In this video, we'll look at some of the motivating factors that led to the inclusion of lamb expressions 2 00:00:13,590 --> 00:00:15,090 in C++ 11. 3 00:00:16,320 --> 00:00:19,380 Lambdas were a tremendous addition to C++. 4 00:00:19,560 --> 00:00:26,400 Prior to C++ 11, we mainly used function objects and function pointers when we needed custom behavior 5 00:00:26,400 --> 00:00:29,760 with the standard template library and also with our own code. 6 00:00:30,330 --> 00:00:35,280 Function objects were used mostly with the standard template library and function pointers were used 7 00:00:35,280 --> 00:00:36,900 more often as callbacks. 8 00:00:37,420 --> 00:00:38,970 Okay, so what's the problem? 9 00:00:39,000 --> 00:00:45,390 Well, one of the problems is that we often ended up writing many short functions that control algorithms. 10 00:00:45,600 --> 00:00:51,870 And in the case of the STL, these small functions were encapsulated in small classes to produce functional 11 00:00:51,870 --> 00:00:52,560 objects. 12 00:00:52,590 --> 00:00:54,480 Now that's okay and it works. 13 00:00:54,480 --> 00:00:59,520 But many times the classes and the functions are defined far away from when they're used. 14 00:00:59,520 --> 00:01:03,480 And this can lead to problems with modifying, maintaining and testing our code. 15 00:01:04,080 --> 00:01:09,780 In addition, the compiler is not so good at optimizing the functions when they're not defined in line. 16 00:01:09,960 --> 00:01:11,280 Let's see an example. 17 00:01:13,940 --> 00:01:18,080 In this example, we'll create a class that will serve to provide function objects. 18 00:01:18,380 --> 00:01:22,370 We'll see a couple of examples in these slides as well as when we go to the ID. 19 00:01:22,490 --> 00:01:25,730 Now, these classes all have the same type of structure. 20 00:01:25,790 --> 00:01:28,760 They might have attributes, constructors and so forth. 21 00:01:28,760 --> 00:01:32,930 But the most important thing to have is an overloaded function call operator. 22 00:01:33,170 --> 00:01:36,770 That's those open and closed persons that you see next to the operator. 23 00:01:37,610 --> 00:01:39,770 You can see them in bold in the slide. 24 00:01:39,800 --> 00:01:46,010 Whenever this operator is used, the function associated with it is called for an object of any multiplier 25 00:01:46,010 --> 00:01:46,520 type. 26 00:01:47,000 --> 00:01:52,670 So in this example, we initialize multiplier objects with an integer and we store that in the num attribute. 27 00:01:52,820 --> 00:01:59,210 Then when we call the overloaded function operator with a multiplier object, we multiply the past in 28 00:01:59,210 --> 00:02:02,150 integer n by num and we return the result. 29 00:02:02,180 --> 00:02:07,460 It's pretty simple, and these classes tend to be small, but depending on our application we might 30 00:02:07,460 --> 00:02:09,560 end up with many variants of them. 31 00:02:09,740 --> 00:02:11,690 So how do you use these classes? 32 00:02:13,990 --> 00:02:16,150 Let's see an example with the steel. 33 00:02:17,380 --> 00:02:23,020 Let's assume that we have that multiplier class that we just defined, and we have a vector called VEC, 34 00:02:23,020 --> 00:02:26,440 which is a vector of integers initialized to one, two, three and four. 35 00:02:26,740 --> 00:02:29,620 So let's create a function object from the multiplier class. 36 00:02:29,620 --> 00:02:31,540 We'll call it mult multi. 37 00:02:31,960 --> 00:02:37,210 When we call the overloaded function call operator on this object, it will return whatever integer 38 00:02:37,210 --> 00:02:39,670 we pass into it multiplied by ten. 39 00:02:40,660 --> 00:02:46,010 So we can call the SQL algorithm transform and provide the iterator for the vector. 40 00:02:46,030 --> 00:02:52,870 What this does is it starts at vector begin and ends at vector end and it starts writing back or transforming 41 00:02:52,870 --> 00:02:54,730 the elements in vector from the beginning. 42 00:02:54,730 --> 00:02:55,450 That's it. 43 00:02:55,570 --> 00:02:58,120 But now what do we do to transform that vector? 44 00:02:58,120 --> 00:03:00,190 Well, that's where the function object comes in. 45 00:03:00,190 --> 00:03:06,940 We iterate through the vector and we pass in each integer we see into mult when we're done. 46 00:03:06,940 --> 00:03:11,170 Vec now has been transformed to ten, 20, 30 and 40. 47 00:03:13,790 --> 00:03:16,550 Now we don't have to create a name multiplier object. 48 00:03:16,550 --> 00:03:19,190 We can simply create an unnamed multiplier object. 49 00:03:19,190 --> 00:03:22,310 As you can see here in bold, we get the same behavior. 50 00:03:22,340 --> 00:03:23,920 So what's wrong with all of this? 51 00:03:23,930 --> 00:03:24,970 Well, nothing, really. 52 00:03:24,980 --> 00:03:29,960 It just becomes tedious to read all these classes, each with the specific behavior in the overloaded 53 00:03:29,960 --> 00:03:31,190 function call operator. 54 00:03:31,340 --> 00:03:35,420 And sometimes it's not so obvious what they're doing when you're looking at the existing code. 55 00:03:37,770 --> 00:03:43,950 Here's another example using a template class to create a generic display, and we can use this with 56 00:03:43,950 --> 00:03:47,490 functional objects of any type that overload the insertion operator. 57 00:03:47,880 --> 00:03:50,070 Notice that we have no attributes in this class. 58 00:03:50,070 --> 00:03:55,770 All we're doing is overloading the function call operator to display a t object to see out. 59 00:03:56,070 --> 00:03:58,170 So how can we use this class template? 60 00:03:58,170 --> 00:03:59,610 It's actually pretty easy. 61 00:04:01,450 --> 00:04:04,890 Let's create two functional objects from the display or template class. 62 00:04:04,900 --> 00:04:10,390 The first will display integers and we'll call it D one and the second will display stud strings and 63 00:04:10,390 --> 00:04:11,560 we'll call it DX two. 64 00:04:12,100 --> 00:04:15,190 Notice how we provide the type and the template parameters. 65 00:04:15,700 --> 00:04:19,510 We cannot use these function objects just as we would use functions. 66 00:04:19,510 --> 00:04:25,810 We can call DX one and pass in 100 as an argument and we can call DX two with Frank as the argument. 67 00:04:26,570 --> 00:04:31,970 Behind the scenes, the overloaded function call operator is called and we display the appropriate result. 68 00:04:32,000 --> 00:04:37,550 Now this example really shows clearly the DX one and DX two are objects, but we're using them like 69 00:04:37,550 --> 00:04:40,760 functions and that's the reason they're called function objects. 70 00:04:40,760 --> 00:04:43,610 But using them as in this example isn't all that useful. 71 00:04:43,610 --> 00:04:44,780 So let's see a better way. 72 00:04:47,920 --> 00:04:53,590 In this example we have one is a vector of the integers one, two, three, four and five and vec two 73 00:04:53,620 --> 00:04:54,940 is a vector of strings. 74 00:04:54,940 --> 00:04:56,230 Larry, moe and curly. 75 00:04:57,300 --> 00:05:01,920 Now we use stood for each to display each of the elements provided by the iterator. 76 00:05:02,040 --> 00:05:06,750 In the first example, we instantiate a display of iNts anonymously. 77 00:05:07,110 --> 00:05:09,270 Notice the function call operator at the end. 78 00:05:09,300 --> 00:05:12,060 That's important because we need to instantiate an object. 79 00:05:12,540 --> 00:05:18,270 In the second example, we use D one, which we created in the previous slide and in the last example 80 00:05:18,270 --> 00:05:19,380 we use DD to. 81 00:05:20,110 --> 00:05:25,150 So now these functional objects display all the elements in the vector, and we only had to write the 82 00:05:25,150 --> 00:05:27,760 class once since we wrote it as a generic template. 83 00:05:27,790 --> 00:05:32,350 That's pretty cool and it works great, but I think using lambda expressions is even cooler. 84 00:05:32,380 --> 00:05:33,850 Let me show you what that looks like. 85 00:05:37,220 --> 00:05:40,100 So now let's do the same thing with Lambda Expressions. 86 00:05:40,490 --> 00:05:43,610 Note that all the code we need to do this is on this slide. 87 00:05:43,610 --> 00:05:44,960 We don't need anything else. 88 00:05:44,960 --> 00:05:48,680 We don't need that display or template class or the function objects. 89 00:05:48,680 --> 00:05:49,850 DX one and dx two. 90 00:05:51,040 --> 00:05:54,280 We do have the same two vectors vec one and vec two. 91 00:05:54,310 --> 00:05:57,590 Now in the for each we don't pass in a function object. 92 00:05:57,610 --> 00:06:00,040 Instead we use a lambda expression. 93 00:06:00,100 --> 00:06:03,040 Don't worry about the syntax of the lambda expression right now. 94 00:06:03,040 --> 00:06:04,930 We'll go over that in detail soon. 95 00:06:04,930 --> 00:06:09,400 But do notice that all the behavior we want is exactly where we want it. 96 00:06:09,400 --> 00:06:11,860 Right in line in the call to forage. 97 00:06:12,100 --> 00:06:17,440 So in this case, we're just displaying the data, but we can easily modify our lambda to do whatever 98 00:06:17,440 --> 00:06:18,490 we want them to. 99 00:06:18,880 --> 00:06:23,830 This makes the code more readable, easier to test, easier to debug, and more maintainable. 100 00:06:24,220 --> 00:06:27,640 Also, the compiler can more easily optimize this code. 101 00:06:29,000 --> 00:06:31,430 So lambdas replace function objects, right. 102 00:06:31,730 --> 00:06:32,530 Yes. 103 00:06:32,540 --> 00:06:35,090 And we write less code and more focused code, right? 104 00:06:35,120 --> 00:06:35,870 Yes. 105 00:06:36,050 --> 00:06:38,720 So we never have to use function objects again, right? 106 00:06:38,990 --> 00:06:39,740 Wrong. 107 00:06:40,130 --> 00:06:45,890 Best practice with lambda expressions is to use them when the amount of code is a statement or a few 108 00:06:45,890 --> 00:06:46,720 statements. 109 00:06:46,730 --> 00:06:51,200 If you have more complex code, you should consider using a function object. 110 00:06:52,230 --> 00:06:58,140 One other benefit of a lambda expression is that the compiler creates a closure object from the lambda 111 00:06:58,140 --> 00:06:58,920 expression. 112 00:06:58,950 --> 00:07:03,570 This means that we can gain access to the environment in which the lambda exists. 113 00:07:03,600 --> 00:07:06,810 This is super powerful and we'll see how it works pretty soon. 114 00:07:07,320 --> 00:07:12,480 So why did I spend time talking about functional objects if we can mostly replace them with lambdas? 115 00:07:12,510 --> 00:07:18,690 Well, it's because the compiler generates unnamed functional objects behind the scenes from the lambda 116 00:07:18,690 --> 00:07:19,620 expressions. 117 00:07:19,620 --> 00:07:21,600 And now you understand how they work. 118 00:07:21,990 --> 00:07:22,410 Great. 119 00:07:22,410 --> 00:07:25,500 So let's head over to the ID and we'll see some of this in live code. 120 00:07:26,560 --> 00:07:26,860 Okay. 121 00:07:26,860 --> 00:07:32,950 So I'm back in the ID, I'm in the Section 21 workspace and I'm in the Function Objects project. 122 00:07:33,860 --> 00:07:36,810 And this project does a little bit of what we just did on the slides. 123 00:07:36,830 --> 00:07:40,940 It's really straightforward, but I really want to go over it to make sure that you understand function 124 00:07:40,940 --> 00:07:43,350 objects, function objects. 125 00:07:43,370 --> 00:07:48,770 I've seen a lot of beginning C++ programmers really not understand these, and I think it's all about 126 00:07:48,770 --> 00:07:51,920 that operator, that function call operator right here. 127 00:07:52,190 --> 00:07:55,310 I don't know why it's just an operator just because it looks like a function. 128 00:07:55,310 --> 00:07:56,300 Don't let it freak you out. 129 00:07:56,300 --> 00:08:00,710 It's just an operator, just like the insertion or the extraction or a plus or minus. 130 00:08:00,710 --> 00:08:01,340 That's it. 131 00:08:01,430 --> 00:08:05,570 It's just the way it's used is a little different, but it should be very, very straightforward and 132 00:08:05,570 --> 00:08:08,600 hopefully intuitive after this demo here. 133 00:08:08,630 --> 00:08:09,000 Okay. 134 00:08:09,020 --> 00:08:13,100 So what we've got here is we've got three classes. 135 00:08:13,520 --> 00:08:14,510 These two are structures. 136 00:08:14,510 --> 00:08:17,480 This one down here is a class multiplier, which I'll get to in a second. 137 00:08:17,480 --> 00:08:18,990 I'll just keep it closed for now. 138 00:08:19,010 --> 00:08:22,910 So what I'm doing here is I'm creating this structure right here. 139 00:08:23,510 --> 00:08:29,570 It's called Square Factor, and all it does is that it overloads the function call operator right there. 140 00:08:29,570 --> 00:08:30,200 That's it. 141 00:08:30,200 --> 00:08:31,340 That's all it's doing. 142 00:08:31,970 --> 00:08:34,880 By doing that, you can not create function objects. 143 00:08:34,880 --> 00:08:37,100 And I'll show you exactly how that works in a second. 144 00:08:37,100 --> 00:08:43,760 So I'm overloading that operator and it expects an integer, and when I get that integer, I'm simply 145 00:08:43,760 --> 00:08:45,080 displaying the square. 146 00:08:45,080 --> 00:08:45,470 That's it. 147 00:08:45,470 --> 00:08:46,550 I'm not changing anything. 148 00:08:46,550 --> 00:08:48,770 I'm only displaying output only. 149 00:08:48,960 --> 00:08:50,690 Okay, so that went straight forward. 150 00:08:50,690 --> 00:08:57,980 And then I've got a template class here that expects my template parameter T and in this case it's called 151 00:08:57,980 --> 00:08:58,850 disk player. 152 00:08:58,880 --> 00:09:01,490 Again, I'm overloading that operator. 153 00:09:01,490 --> 00:09:03,230 Write the function call operator. 154 00:09:03,230 --> 00:09:05,420 It expects a T, whatever that is. 155 00:09:05,420 --> 00:09:05,960 Could be in it. 156 00:09:05,960 --> 00:09:08,000 It could be a string, it could be just about anything. 157 00:09:08,000 --> 00:09:10,940 And all I'm doing again is outputting the data. 158 00:09:11,090 --> 00:09:16,970 So in this case, we have to make sure that we are overloading that insertion operator with whatever 159 00:09:16,970 --> 00:09:20,990 type we're using, I'm only going to be using instant strings, so I know it's going to work just fine 160 00:09:20,990 --> 00:09:21,500 with them. 161 00:09:21,860 --> 00:09:23,270 Okay, so let's go over main. 162 00:09:23,270 --> 00:09:24,950 Here is my test one. 163 00:09:24,950 --> 00:09:27,890 You can see the output right up here on the right. 164 00:09:27,950 --> 00:09:32,930 And what I'm doing is I'm creating an object and that object's name is square. 165 00:09:33,230 --> 00:09:34,880 It's type is square function. 166 00:09:34,880 --> 00:09:36,940 It's one of these guys, right? 167 00:09:36,950 --> 00:09:40,550 The only thing I can do with that object is call this function call operator. 168 00:09:40,550 --> 00:09:40,940 That's it. 169 00:09:40,940 --> 00:09:41,900 There's nothing else to do. 170 00:09:41,900 --> 00:09:44,450 There's no attributes, there's no other methods. 171 00:09:44,450 --> 00:09:47,480 So this is about as simple a function object as you can get. 172 00:09:47,480 --> 00:09:52,490 So now, once I have square, I can say square and pass in a four. 173 00:09:52,520 --> 00:09:55,550 Now, this looks exactly like a function call, doesn't it? 174 00:09:55,640 --> 00:09:57,170 And that's the whole point. 175 00:09:57,710 --> 00:10:02,420 What's really happening behind the scenes is, remember, this square is not a function, it's an object. 176 00:10:02,420 --> 00:10:08,300 So what's really happening behind the scenes is that we're calling squares dot operator function call 177 00:10:08,420 --> 00:10:10,100 and I'm passing in the four. 178 00:10:11,260 --> 00:10:12,330 Well, hopefully that's clear. 179 00:10:12,330 --> 00:10:14,550 It's really important that you understand this piece right here. 180 00:10:14,550 --> 00:10:20,490 This is about a simple and example as I can come up with, because remember, lambdas are converted 181 00:10:20,490 --> 00:10:22,920 into these functional objects behind the scenes. 182 00:10:22,920 --> 00:10:27,180 So in this case, we display a 16 and you can see the 16 displaying right here. 183 00:10:27,660 --> 00:10:30,780 In this case, I'm creating two function objects. 184 00:10:30,780 --> 00:10:33,240 One is called D one, the other one is called DD two. 185 00:10:33,240 --> 00:10:37,710 I'm using this template class to do it, so I need to pass in my template parameter. 186 00:10:37,710 --> 00:10:42,420 So this one dd one will display ints dd two will display std strings. 187 00:10:42,810 --> 00:10:44,610 All we're doing here is outputting the data. 188 00:10:44,610 --> 00:10:45,930 So it's going to be really simple. 189 00:10:45,930 --> 00:10:52,410 We call DD one with 100, DD two with Frank and we get right out here, 100 and Frank again. 190 00:10:52,410 --> 00:10:54,540 These guys look just like function calls, right? 191 00:10:54,540 --> 00:10:55,260 But they're not. 192 00:10:55,260 --> 00:10:56,490 They're function objects. 193 00:10:56,490 --> 00:11:04,110 What's happening is d one operator function call operator 100 DD two operator using the function call 194 00:11:04,110 --> 00:11:05,910 operator again, we're passing in the string. 195 00:11:06,810 --> 00:11:11,820 So again, that's about as simple an example as I can come up with that really drives home the point 196 00:11:11,820 --> 00:11:12,240 here. 197 00:11:12,240 --> 00:11:13,710 They look like function calls. 198 00:11:13,710 --> 00:11:15,360 They're not they're function objects. 199 00:11:15,810 --> 00:11:18,060 All right, so let's move on to test two. 200 00:11:19,500 --> 00:11:25,440 And what we'll do here is we'll use these function objects with our standard template algorithms. 201 00:11:25,440 --> 00:11:27,690 So here is my test two right here. 202 00:11:27,690 --> 00:11:30,060 And you can see the output beginning up here on the right. 203 00:11:30,990 --> 00:11:33,920 All I'm doing is creating vec one as an integer vector. 204 00:11:33,990 --> 00:11:39,140 One, two, three, four, five and VEC two is a vector of strings, Larry, Moe and Curly. 205 00:11:39,150 --> 00:11:40,470 That's what we're going to work with. 206 00:11:40,470 --> 00:11:45,150 And what we're going to do now is we're going to use the for each algorithm right here. 207 00:11:46,020 --> 00:11:48,330 Remember, it's an algorithm, so it needs an iterator. 208 00:11:48,330 --> 00:11:48,990 Here's the iterator. 209 00:11:48,990 --> 00:11:51,600 I'm going to go from the beginning to the end of vector one. 210 00:11:51,600 --> 00:11:53,310 That's one, two, three, four, five. 211 00:11:53,310 --> 00:11:59,790 And at each iteration, the algorithm will pass in whatever the element is in that collection. 212 00:11:59,790 --> 00:11:59,940 Right? 213 00:11:59,940 --> 00:12:03,450 And that container, which is an integer, it's going to pass it into square. 214 00:12:03,630 --> 00:12:06,510 So just like we did here, that's what's going to happen behind the scenes. 215 00:12:07,170 --> 00:12:11,430 So it's going to pass in the one, the two, the three, the four and the five in that order. 216 00:12:11,520 --> 00:12:17,040 And all we're doing with this func to right here with this function object is displaying. 217 00:12:17,040 --> 00:12:20,400 So it's going to display the square of each of the numbers passed into it. 218 00:12:20,400 --> 00:12:23,760 149 1625 you can see the output right there. 219 00:12:23,760 --> 00:12:27,390 This does not change the vector, we're only displaying the vector. 220 00:12:28,710 --> 00:12:31,740 So here we're going to display what we just did. 221 00:12:31,740 --> 00:12:34,380 And actually this this one is that guy right there. 222 00:12:34,920 --> 00:12:35,120 Right? 223 00:12:35,160 --> 00:12:36,930 This this does no display right there. 224 00:12:36,930 --> 00:12:42,090 So here for each again vec one begin to vec one end called dx one. 225 00:12:42,090 --> 00:12:44,350 What's dx one will dx one is that display. 226 00:12:44,430 --> 00:12:47,190 All it does is output the value passed into it. 227 00:12:47,190 --> 00:12:51,420 So it's going to be passed in one, two, three, four, five and it's going to display them right here. 228 00:12:52,080 --> 00:12:57,330 You can see it right there in this case, a little bit different. 229 00:12:57,330 --> 00:13:01,860 What I'm doing is I'm using the for each and here I'm using vector two, which is Larry, Moe and Curly. 230 00:13:01,860 --> 00:13:07,230 I'm going from the beginning to the end and I'm creating a display or object right there. 231 00:13:07,620 --> 00:13:08,730 I'm doing that in place. 232 00:13:08,730 --> 00:13:12,870 I'm not naming it like I did DX one, DX two so I'm just calling display. 233 00:13:13,380 --> 00:13:17,940 I'm using the template parameter stood string and notice right here, those are important because that's 234 00:13:17,940 --> 00:13:19,350 what instantiating it. 235 00:13:19,350 --> 00:13:22,410 That's not the function call operator, that's a constructor. 236 00:13:22,410 --> 00:13:24,300 So that's creating that object. 237 00:13:24,390 --> 00:13:27,750 So now I've got a display or object that can display strings. 238 00:13:27,750 --> 00:13:32,310 I'll loop through the vector using the algorithm every time it's going to pass in an integer and it's 239 00:13:32,310 --> 00:13:36,540 going to display Larry, Moe and Curly that's right there. 240 00:13:38,100 --> 00:13:42,870 And then down here we're doing the same thing, except we're using the DX to function object, which 241 00:13:42,870 --> 00:13:44,250 is the one we created earlier. 242 00:13:44,340 --> 00:13:46,110 And it does exactly the same thing. 243 00:13:46,110 --> 00:13:47,880 It just prints out Larry, Moe and Curly. 244 00:13:48,660 --> 00:13:56,400 So now let's go on to test three and we're right here on line 62. 245 00:13:57,600 --> 00:14:00,690 So what we're doing again now, this time we're going to use lambdas. 246 00:14:00,690 --> 00:14:06,150 So all the stuff we just wrote up top, all those template classes and that square function, all that 247 00:14:06,150 --> 00:14:07,740 stuff, we're not using any of that now. 248 00:14:07,740 --> 00:14:09,450 We're doing it all using lambdas. 249 00:14:09,450 --> 00:14:15,750 So this code right here is all self contained, it needs nothing else, which is kind of cool. 250 00:14:15,810 --> 00:14:21,750 So right here we're using for each and we're going from vector one beginning vector one end. 251 00:14:21,750 --> 00:14:23,580 And here's my lambda expression. 252 00:14:23,580 --> 00:14:26,070 It starts here with those square brackets like that. 253 00:14:26,070 --> 00:14:30,900 We'll get used to that syntax in a second and I'll talk about the syntax in detail in the next video. 254 00:14:30,930 --> 00:14:32,550 I just wanted to show you what it looked like. 255 00:14:32,610 --> 00:14:34,560 And Lambda ends right there. 256 00:14:34,560 --> 00:14:40,500 So now what's going to happen is the for each algorithm will send in an integer right in here to the 257 00:14:40,500 --> 00:14:43,890 lambda and it's x and all we're doing is just displaying X squared. 258 00:14:43,890 --> 00:14:48,240 We're not changing x, we're not changing the vector, we're simply displaying the elements. 259 00:14:48,240 --> 00:14:50,150 The same thing we did with the display. 260 00:14:51,210 --> 00:14:52,500 So that's it. 261 00:14:52,500 --> 00:14:57,660 What's going to happen is it's going to print out one four, nine, 16, 25 and you can see that right 262 00:14:57,660 --> 00:14:58,980 here in test three. 263 00:14:59,820 --> 00:15:01,470 Remember, we haven't changed the vector. 264 00:15:02,280 --> 00:15:07,620 Now, if we wanted to take each one of those integers that's being passed into the lambda and multiply 265 00:15:07,620 --> 00:15:09,900 it by ten, we could just do that right there. 266 00:15:09,900 --> 00:15:12,360 So you hopefully you can see how clear this is. 267 00:15:12,750 --> 00:15:14,310 It may not be clear the first time you see it. 268 00:15:14,310 --> 00:15:18,150 I remember the first time I saw Lambdas and I've used them in other languages as well. 269 00:15:18,540 --> 00:15:22,890 The syntax is always a little tricky at the beginning, but after a little bit it becomes second nature. 270 00:15:22,890 --> 00:15:28,500 So what's going to happen is the for each will pass in each integer as x to the lambda expression. 271 00:15:28,500 --> 00:15:32,370 In all this lambda expression it is doing is multiplying it by ten and displaying it. 272 00:15:32,370 --> 00:15:32,970 That's it. 273 00:15:32,970 --> 00:15:34,140 It's not changing anything. 274 00:15:34,140 --> 00:15:36,510 It's just multiplying it by ten and displaying it. 275 00:15:36,510 --> 00:15:40,080 So in this case, we're going to get ten, 20, 30, 40 and 50. 276 00:15:40,110 --> 00:15:42,750 Here we're just displaying X. 277 00:15:42,760 --> 00:15:46,830 You can see how we're displaying one, two, three, four and five, which tells you that that vector 278 00:15:46,830 --> 00:15:48,090 was never changed. 279 00:15:48,090 --> 00:15:49,260 And that was the whole point. 280 00:15:50,040 --> 00:15:55,260 And then in the last example, we're using Vector now, which is a vector of strings. 281 00:15:55,470 --> 00:16:00,570 Notice the parameter that's being passed into the lambda is a stood string now instead of an int. 282 00:16:00,570 --> 00:16:02,910 We did the int up here, we did the string here. 283 00:16:03,000 --> 00:16:07,050 This is really nice that you can code what you want right in here. 284 00:16:07,050 --> 00:16:08,490 This is what I want to do. 285 00:16:09,030 --> 00:16:10,200 I'm coding it right in there. 286 00:16:10,200 --> 00:16:11,280 That's all I need to do. 287 00:16:11,280 --> 00:16:15,810 I don't need to look through my source files to figure out where my function, my function, object 288 00:16:15,810 --> 00:16:17,310 class is or anything like that. 289 00:16:17,310 --> 00:16:21,420 It's right in here, right in place, which is kind of cool in this case. 290 00:16:21,420 --> 00:16:24,300 I'm just displaying Larry, Moe and Curly and you can see them right there. 291 00:16:25,680 --> 00:16:25,980 All right. 292 00:16:25,980 --> 00:16:27,540 So now let's do the last one. 293 00:16:27,540 --> 00:16:30,330 And in order to do the last one, we're using this multiplier. 294 00:16:30,490 --> 00:16:30,790 Last. 295 00:16:30,800 --> 00:16:32,800 Let me scroll up and I'll show you that. 296 00:16:32,800 --> 00:16:35,540 So this one's a little bit more complex, but not much. 297 00:16:35,560 --> 00:16:37,960 This is the same one we did on the slides here. 298 00:16:37,960 --> 00:16:42,460 I've got a class and it's got a private attribute right here called NUM. 299 00:16:43,180 --> 00:16:45,250 It's got a constructor right here. 300 00:16:45,250 --> 00:16:51,940 You can see the user passes in an RN and all we're doing is assigning end to that attribute right there. 301 00:16:51,940 --> 00:16:52,570 That's it. 302 00:16:52,570 --> 00:16:57,850 We're just setting that instance variable and to notice right here, we're overloading the function 303 00:16:57,850 --> 00:16:58,750 call operator. 304 00:16:58,750 --> 00:17:02,620 That's what makes it a function object when we create function objects from this class. 305 00:17:02,770 --> 00:17:09,339 Okay, best practice is when you've got variables or attributes in here and you don't want them change. 306 00:17:09,339 --> 00:17:11,020 Make sure you put constant here. 307 00:17:11,829 --> 00:17:12,430 So that's it. 308 00:17:12,430 --> 00:17:17,770 So every time you call the function call operator on, multiply your objects, it's going to take in 309 00:17:17,770 --> 00:17:21,910 whatever you pass in which is n and multiply it by whatever we set num to. 310 00:17:21,940 --> 00:17:23,109 That's all it's doing. 311 00:17:23,349 --> 00:17:25,119 It's a real, real simple multiplier. 312 00:17:25,690 --> 00:17:30,100 All right, so now let's go down to my test for right here. 313 00:17:30,100 --> 00:17:34,810 You can see right on line 75 is where it begins and the output is right over here. 314 00:17:35,200 --> 00:17:40,570 I'm creating a function object right here called mult and I'm passing in 100 to it. 315 00:17:40,780 --> 00:17:44,950 So remember, I could just simply pass anything into that. 316 00:17:44,950 --> 00:17:46,590 Now we're using the function call operator. 317 00:17:46,750 --> 00:17:49,180 Whatever I pass in, it's going to multiply it by 100. 318 00:17:49,180 --> 00:17:54,220 If I wanted to multiply it by 30, I'd create another function object mult one and pass it to 30 to 319 00:17:54,220 --> 00:17:55,030 it for example. 320 00:17:55,420 --> 00:17:57,010 So let's see what the how this works. 321 00:17:57,110 --> 00:18:03,050 I'm assigning one, two, three, four to my vector one and now I'm going to use to transform steward 322 00:18:03,130 --> 00:18:04,990 transform does change the vector. 323 00:18:04,990 --> 00:18:06,190 So it's a little bit different. 324 00:18:06,430 --> 00:18:10,810 I think we used it once or twice in the steel section, but I'll go over it real quick. 325 00:18:10,810 --> 00:18:12,340 So here's to transform. 326 00:18:12,340 --> 00:18:16,840 It wants where to start, where to end and where to start making changes. 327 00:18:16,840 --> 00:18:18,850 That's why you have these three iterations here. 328 00:18:18,850 --> 00:18:20,320 So we have where to start. 329 00:18:20,320 --> 00:18:22,360 I'm going to start at vector one begin. 330 00:18:22,780 --> 00:18:25,240 I'm going to end at vector one's end. 331 00:18:25,480 --> 00:18:29,620 Basically, I'm going to iterate through the entire vector one, which is again, one, two, three, 332 00:18:29,620 --> 00:18:33,220 four and I'm going to start making changes in VEC one begin. 333 00:18:33,220 --> 00:18:35,590 So it's going to start making changes here and here and here and here. 334 00:18:35,590 --> 00:18:36,640 It's going to change all of them. 335 00:18:36,820 --> 00:18:37,960 And what's it changing? 336 00:18:37,960 --> 00:18:40,960 Well, it's using that object. 337 00:18:40,960 --> 00:18:45,070 So what's going to happen is it's going to go through vector one, it's going to grab the one and it's 338 00:18:45,070 --> 00:18:46,480 going to pass it into moult. 339 00:18:46,480 --> 00:18:47,740 What's it going to do? 340 00:18:47,770 --> 00:18:53,230 It's going to take that guy and multiply it by 100 and it's going to stick it back into the vector one's 341 00:18:53,230 --> 00:18:53,920 position. 342 00:18:53,950 --> 00:18:55,030 So it's going to make that 100. 343 00:18:55,030 --> 00:18:57,250 It's actually going to transform the vector. 344 00:18:57,550 --> 00:19:01,600 And so when we're done with this, we're just going to display it. 345 00:19:01,600 --> 00:19:04,780 And you can see the display right here, 102 hundred, 301 hundred. 346 00:19:04,780 --> 00:19:06,910 It actually changed that vector. 347 00:19:06,910 --> 00:19:08,380 And then let's do it again. 348 00:19:08,380 --> 00:19:10,780 What we're doing here is we're resetting that vector. 349 00:19:10,780 --> 00:19:12,100 One, two, one, two, three, four. 350 00:19:12,110 --> 00:19:14,080 And I'm just assigning one, two, three, four to it. 351 00:19:14,080 --> 00:19:17,500 And what we're going to do now is we're going to do exactly the same thing, except we're not going 352 00:19:17,500 --> 00:19:18,990 to use the function object molt. 353 00:19:19,000 --> 00:19:20,530 We're just going to do it in a lambda. 354 00:19:20,530 --> 00:19:21,910 Here's my lambda. 355 00:19:23,620 --> 00:19:29,830 I'm using you to transform again starting at the beginning, going to the end of week one and start 356 00:19:29,830 --> 00:19:32,380 transforming from the beginning of VEC one. 357 00:19:32,380 --> 00:19:38,620 So every time I get an integer, all I'm going to do is return that integer times 100 and it's going 358 00:19:38,620 --> 00:19:40,420 to be stored back into that vector. 359 00:19:40,420 --> 00:19:41,950 It's going to transform the vector. 360 00:19:41,950 --> 00:19:47,380 And now when I use my for each again and notice over here when we did the for each, we use that display 361 00:19:47,380 --> 00:19:48,550 or function object. 362 00:19:48,640 --> 00:19:53,050 This time we're using another lambda that just due to displaying all we're doing is just outputting 363 00:19:53,050 --> 00:19:55,000 x, simple as that. 364 00:19:55,000 --> 00:19:57,880 And when we do that you'll get exactly the same result. 365 00:19:57,880 --> 00:19:59,590 102 hundred and 304 hundred. 366 00:20:00,770 --> 00:20:04,870 So this gives you a little bit of a taste for function objects and lambdas in the next video. 367 00:20:04,880 --> 00:20:07,520 What we'll do is we'll take this syntax right here. 368 00:20:07,700 --> 00:20:09,440 This is actually all you need to remember. 369 00:20:09,440 --> 00:20:11,060 Square brackets that. 370 00:20:12,680 --> 00:20:13,210 That's it. 371 00:20:13,220 --> 00:20:14,710 That's that's the lambda. 372 00:20:15,290 --> 00:20:16,460 And you can see the syntax. 373 00:20:16,460 --> 00:20:17,420 There's my capsule list. 374 00:20:17,420 --> 00:20:19,100 This is where my parameters come in. 375 00:20:19,100 --> 00:20:20,260 There's my code. 376 00:20:20,270 --> 00:20:22,340 Now, there's a couple of other bits that you can throw in here. 377 00:20:22,340 --> 00:20:23,060 You could do that. 378 00:20:23,060 --> 00:20:27,290 You can throw some keywords in there and we'll talk about all of that in the next video when we go over 379 00:20:27,290 --> 00:20:29,810 the structure of the expression in detail. 380 00:20:30,140 --> 00:20:31,100 So I'll see you there.