1 00:00:05,230 --> 00:00:06,010 Hello, everyone. 2 00:00:06,010 --> 00:00:06,970 Welcome back. 3 00:00:07,060 --> 00:00:12,010 In this video, I'm going to talk about Stateful Lambdas and I'm going to show you some examples in 4 00:00:12,010 --> 00:00:12,910 the ID. 5 00:00:13,180 --> 00:00:19,390 I'm in the Section 21 workspace, as you can see right here, and I'm in the Stateful Lambdas Project. 6 00:00:19,780 --> 00:00:23,170 So I've got ten different test functions that I'm going to show you. 7 00:00:23,170 --> 00:00:27,730 Each one does something just a little bit differently, and the idea is to give you a flavor for what 8 00:00:27,730 --> 00:00:31,270 stateful lambdas can do and different ways that they can do things. 9 00:00:31,270 --> 00:00:34,180 So let's go over this first test example. 10 00:00:34,180 --> 00:00:37,810 I've already gone ahead and built this and run it and all test cases over here running. 11 00:00:37,810 --> 00:00:42,670 So I'm just going to walk you right through them, including either stream vector and algorithm, which 12 00:00:42,670 --> 00:00:43,570 we'll use later. 13 00:00:43,780 --> 00:00:46,060 And let me just scroll up just a little bit here. 14 00:00:46,060 --> 00:00:46,390 All right. 15 00:00:46,390 --> 00:00:52,750 So notice right here that we have a variable called Global X and I've initialized it to 1000. 16 00:00:52,780 --> 00:00:59,350 That variable is global to this file, so it's accessible anywhere in this file. 17 00:00:59,350 --> 00:01:02,050 However, I can't capture that kind of variable. 18 00:01:02,410 --> 00:01:04,090 We can't capture those kinds of variables. 19 00:01:04,090 --> 00:01:05,500 We can't count global variables. 20 00:01:05,500 --> 00:01:08,110 For example, we can't capture static variables. 21 00:01:08,110 --> 00:01:13,450 So if you try to capture that variable in the capture list right here, I'll talk about that in a minute. 22 00:01:13,660 --> 00:01:18,190 The compiler is going to give you a warning about only automatic variables can be captured. 23 00:01:18,250 --> 00:01:20,620 And that's all about the reaching scope of the lambda. 24 00:01:20,830 --> 00:01:22,060 What can the lambda see? 25 00:01:22,060 --> 00:01:26,050 So let me clear this and let's go over this one step at a time. 26 00:01:26,140 --> 00:01:29,530 So remember that we've got that Global X out there and it's 1000. 27 00:01:29,620 --> 00:01:33,790 Now, inside this test function, I have a local variable called X, right? 28 00:01:33,790 --> 00:01:36,580 It's called local X and it's set to 100. 29 00:01:36,790 --> 00:01:43,270 Now I've got my lambda expression, which I'm assigning to this variable L and notice it's got a CAPTCHA, 30 00:01:43,270 --> 00:01:43,750 right? 31 00:01:43,750 --> 00:01:46,420 Those square brackets, that's where we put all our captures. 32 00:01:46,420 --> 00:01:50,320 So this makes this guy a state full lambda, not a state less lambda. 33 00:01:50,620 --> 00:01:53,050 In this case, we're capturing local X. 34 00:01:53,500 --> 00:01:55,120 That's that guy right there. 35 00:01:55,120 --> 00:01:57,910 Now, by default, we are capturing by value. 36 00:01:57,910 --> 00:01:59,620 That's the default capture mode. 37 00:01:59,620 --> 00:02:06,040 What that means is that this lambda is going to have a copy of local X in it. 38 00:02:07,030 --> 00:02:12,370 So this is called this guy local X and it is going to be a copy. 39 00:02:13,290 --> 00:02:14,820 Of what it captured. 40 00:02:15,240 --> 00:02:16,740 So it's going to have 100 it. 41 00:02:16,920 --> 00:02:18,740 Let me write that really large. 42 00:02:18,740 --> 00:02:19,710 So there's no confusion. 43 00:02:19,710 --> 00:02:21,540 This is a copy in this case. 44 00:02:21,540 --> 00:02:23,300 I'm making a copy of that integer. 45 00:02:23,310 --> 00:02:29,160 If this variable here were to be an object, then we're going to call a copy constructor to make a copy. 46 00:02:29,160 --> 00:02:31,830 So it's really important to understand that we're making a copy. 47 00:02:32,810 --> 00:02:36,850 Then we're just outputting the local access value in the global access value. 48 00:02:36,860 --> 00:02:43,730 So now when we call that lambda, what's going to happen is this code right here will execute the local 49 00:02:43,730 --> 00:02:48,620 x will display this local x, that's the copy and it's going to display 100. 50 00:02:48,620 --> 00:02:53,180 And you can see right up here my test one, it's going to display 100 and then we're going to display 51 00:02:53,180 --> 00:02:58,190 Global X and it's just finding that just because of normal scope rules, it's going to find global X, 52 00:02:58,190 --> 00:03:00,950 it's 1000 and it's going to display 1000 right here. 53 00:03:00,980 --> 00:03:01,760 Pretty straightforward. 54 00:03:01,760 --> 00:03:07,920 Now, if we try to modify local X inside the Lambda body, we're not going to be able to. 55 00:03:07,970 --> 00:03:12,860 So in order to do that, we need to use the mutable keyword, and I'm going to do that in the next example. 56 00:03:12,860 --> 00:03:16,370 So let me scroll up just a little bit and I'm going to scroll here to test two. 57 00:03:16,370 --> 00:03:17,750 I want to scroll up here as well. 58 00:03:17,750 --> 00:03:22,250 So right now we're talking about test two, which is that output right over here. 59 00:03:22,280 --> 00:03:22,540 Okay. 60 00:03:22,610 --> 00:03:24,320 So let's take a look at test two. 61 00:03:24,380 --> 00:03:29,360 Test two, we have a local variable X and here is our lambda. 62 00:03:29,360 --> 00:03:31,490 We're capturing that local variable X. 63 00:03:31,490 --> 00:03:33,980 So that means that this lambda has an X. 64 00:03:34,930 --> 00:03:38,230 And it's a copy of this X, so I'm going to put 100 in here. 65 00:03:39,270 --> 00:03:39,490 Okay. 66 00:03:39,570 --> 00:03:40,770 That's really important. 67 00:03:40,890 --> 00:03:46,690 Now, when we execute the body, we're notice we're changing that x, we're incrementing it by 100. 68 00:03:46,710 --> 00:03:51,150 It is allowed now because I'm using mutable, because I have that mutable keyword. 69 00:03:51,150 --> 00:03:52,620 It's allowing me to do that. 70 00:03:52,620 --> 00:03:59,070 So what's going to happen here is I'm going to increment the local value, not this guy, the local, 71 00:03:59,070 --> 00:03:59,850 the copy. 72 00:03:59,850 --> 00:04:01,650 I'm going to increment it by 100. 73 00:04:01,650 --> 00:04:05,460 So this guy now becomes 200 and I'm going to display that. 74 00:04:05,460 --> 00:04:12,120 So when I call the lambda, this body executes it, just incremented that to 200 and we're going to 75 00:04:12,120 --> 00:04:15,150 display X, which is 200 right there. 76 00:04:16,480 --> 00:04:20,860 Now when I leave, remember I just called that lamp to here and now I'm back and I'm going to display 77 00:04:20,860 --> 00:04:21,380 X. 78 00:04:21,399 --> 00:04:27,640 Well, the only x that this statement has access to is this X, and that X has not been changed because 79 00:04:27,640 --> 00:04:28,320 we copied it. 80 00:04:28,330 --> 00:04:30,040 Remember, this was a copy here. 81 00:04:31,290 --> 00:04:32,390 That was a copy. 82 00:04:32,400 --> 00:04:34,230 So in this case, it hasn't changed. 83 00:04:34,230 --> 00:04:36,600 So what's going to display is 100. 84 00:04:37,290 --> 00:04:40,050 Now, this is the piece that's really important to understand. 85 00:04:40,050 --> 00:04:45,090 And a lot of students and a lot of people in general don't understand how this works with lambdas. 86 00:04:45,090 --> 00:04:46,530 But it's really important to understand. 87 00:04:46,560 --> 00:04:47,700 Notice what happens here. 88 00:04:47,700 --> 00:04:49,340 I'm calling Lambda again. 89 00:04:49,350 --> 00:04:50,940 I'm calling that lambda elegant. 90 00:04:50,940 --> 00:04:53,730 So I'm going to call this lambda again. 91 00:04:54,180 --> 00:04:57,200 What happens is the body of the lambda executes. 92 00:04:57,210 --> 00:04:59,190 We do not capture again. 93 00:04:59,190 --> 00:05:02,760 The capture only happened here when we initialize that variable. 94 00:05:02,790 --> 00:05:04,980 L So we do not capture the 100. 95 00:05:04,980 --> 00:05:10,920 Again, that lambda expression already has that local x and it's right here and it's 200. 96 00:05:11,370 --> 00:05:11,700 All right. 97 00:05:11,700 --> 00:05:12,720 So again, we're right here. 98 00:05:12,720 --> 00:05:13,920 I'm calling this lambda. 99 00:05:13,920 --> 00:05:19,590 It's going to increment this guy now to 300 and it's going to display 300 right there. 100 00:05:20,250 --> 00:05:25,530 Now, when I come back here, I display X, which is this X that's local to this test to function, 101 00:05:25,530 --> 00:05:27,060 and it's going to display 100. 102 00:05:27,090 --> 00:05:29,700 Hopefully that makes sense if it doesn't make sense. 103 00:05:29,730 --> 00:05:31,920 Go back to the slides and look at the slides. 104 00:05:31,920 --> 00:05:34,080 And it's really important that you understand what's happening. 105 00:05:34,080 --> 00:05:36,240 Remember, that's the state for Lambda. 106 00:05:36,240 --> 00:05:40,890 When we have that lambda, remember that class that got created? 107 00:05:41,700 --> 00:05:44,330 Remember we talked about that close your class that was created. 108 00:05:44,360 --> 00:05:47,540 It has a constructor and it has. 109 00:05:48,310 --> 00:05:50,500 The function call operator. 110 00:05:50,710 --> 00:05:52,240 This would be the constructor. 111 00:05:53,080 --> 00:05:54,400 Right here. 112 00:05:54,550 --> 00:05:56,620 The constructor will be called. 113 00:05:57,220 --> 00:05:58,440 Right here. 114 00:05:58,450 --> 00:06:03,340 The operator function will be called right here. 115 00:06:03,340 --> 00:06:04,540 The operator. 116 00:06:05,650 --> 00:06:07,390 The function call operator will be called. 117 00:06:07,420 --> 00:06:09,170 That's really important to understand. 118 00:06:09,190 --> 00:06:13,420 The only time that we capture is when that constructor is executed. 119 00:06:13,540 --> 00:06:15,310 And that's only going to be executed once. 120 00:06:15,310 --> 00:06:17,650 Remember, we can only initialize an object once. 121 00:06:17,650 --> 00:06:19,460 And that's exactly what LL is. 122 00:06:19,480 --> 00:06:20,770 It's an object. 123 00:06:20,770 --> 00:06:22,360 So, again, take your time with that. 124 00:06:22,360 --> 00:06:25,630 Wrap your head around it, because it's really, really important to understand what's happening there. 125 00:06:25,680 --> 00:06:27,910 Okay, so let's take a look at test three. 126 00:06:27,910 --> 00:06:32,370 Now, what's happening with Test three is we're capturing by reference. 127 00:06:32,410 --> 00:06:34,630 So again, I've got my X right here. 128 00:06:34,630 --> 00:06:35,580 It's 100. 129 00:06:35,620 --> 00:06:39,360 Now we're capturing that X by reference and that's the syntax right there. 130 00:06:39,460 --> 00:06:41,890 Using that ampersand, the referencing operator. 131 00:06:41,890 --> 00:06:44,620 So what happens now is and it's mutable as well. 132 00:06:44,770 --> 00:06:50,530 What we're going to do now is that X is local and it's not really an X, right. 133 00:06:50,530 --> 00:06:53,440 We have a reference that's really important to understand as well. 134 00:06:53,440 --> 00:06:57,610 So whenever we change X, we're actually changing the actual X right here. 135 00:06:57,700 --> 00:06:58,780 So what do we do here? 136 00:06:58,780 --> 00:07:04,450 The body increments X by 100 and we display X, so let's call it what's going to happen. 137 00:07:04,450 --> 00:07:06,490 It's going to be it's already captured x, right? 138 00:07:06,490 --> 00:07:11,340 Its value is a reference to the actual we're going to increment it by 100. 139 00:07:11,350 --> 00:07:14,020 What we're going to do is we're going to increment not any local copy. 140 00:07:14,020 --> 00:07:14,790 There is none. 141 00:07:14,800 --> 00:07:18,820 We're going to increment this guy by 100 and we're going to display it. 142 00:07:18,940 --> 00:07:23,050 So that's going to display 200 here and you can see it right up there. 143 00:07:23,870 --> 00:07:28,730 Now when we return from the function call, we're going to display this X right here. 144 00:07:30,030 --> 00:07:33,060 It has been changed because we caught we captured by reference. 145 00:07:33,060 --> 00:07:35,920 So that's also going to display 200 OC. 146 00:07:35,940 --> 00:07:39,450 So that's three examples that are a little bit different from one another, but hopefully you'll see 147 00:07:39,450 --> 00:07:40,170 what's going on. 148 00:07:40,170 --> 00:07:40,530 All right. 149 00:07:40,530 --> 00:07:43,620 So now in this case, we're going to use default capture. 150 00:07:44,580 --> 00:07:48,780 By value and we're going to do it mutable just to make it more fun so we can actually change something 151 00:07:48,780 --> 00:07:50,670 and you can really see what's happening. 152 00:07:50,670 --> 00:07:57,390 So here we're in test four and we've got these three local variables to this test for function X, Y 153 00:07:57,390 --> 00:07:59,520 and Z 102 hundred and 300. 154 00:07:59,580 --> 00:08:05,880 Here's our lambda variable and we are capturing everything by value. 155 00:08:06,360 --> 00:08:09,000 So that's the default capture by value. 156 00:08:09,000 --> 00:08:12,870 So we're capturing X, Y and Z by value. 157 00:08:12,900 --> 00:08:19,530 What's going to happen here is I'm going to make a copy of X, I'm going to make a copy of Y and I'm 158 00:08:19,530 --> 00:08:21,090 going to make a copy of Z. 159 00:08:21,690 --> 00:08:25,200 So X is 100, so I'm going to make that copy. 160 00:08:25,620 --> 00:08:29,160 Y is 200 and Z is 300. 161 00:08:29,730 --> 00:08:31,080 But now notice something. 162 00:08:31,080 --> 00:08:34,380 We're not using Z in this code at all. 163 00:08:34,380 --> 00:08:36,360 So the compiler is not going to capture it. 164 00:08:36,390 --> 00:08:38,700 Why capture something that you're not going to use? 165 00:08:38,700 --> 00:08:40,380 So this won't happen. 166 00:08:40,409 --> 00:08:42,240 It's not going to waste its time doing that. 167 00:08:42,240 --> 00:08:47,550 So the only thing that's captured is X and Y and you're going to get a warning in the output when you 168 00:08:47,550 --> 00:08:53,250 build this program saying the variable Z was not captured since it's not being used some kind of unused 169 00:08:53,250 --> 00:08:54,090 variable warning. 170 00:08:54,090 --> 00:08:56,160 So again, by value, right? 171 00:08:56,160 --> 00:08:58,050 So again, we've made copies. 172 00:08:58,200 --> 00:09:00,660 Now I'm going to increment X by 100. 173 00:09:00,870 --> 00:09:03,030 So X is now 200. 174 00:09:03,840 --> 00:09:11,010 Remember this X here, not the sex here because we're capturing by value y is incremented by 100. 175 00:09:11,010 --> 00:09:12,900 So I'm sorry, it's 200 here. 176 00:09:13,200 --> 00:09:13,890 No, that's correct. 177 00:09:13,890 --> 00:09:14,970 Y was 200. 178 00:09:14,970 --> 00:09:16,410 Now Y is 300. 179 00:09:16,410 --> 00:09:17,220 That's correct. 180 00:09:17,700 --> 00:09:19,440 And now I'm going to output X and Y. 181 00:09:19,440 --> 00:09:21,720 Right now let's call the lambda. 182 00:09:21,750 --> 00:09:22,860 This will execute. 183 00:09:22,860 --> 00:09:23,700 It gives us this. 184 00:09:23,700 --> 00:09:29,130 And what we're going to display is right here, 203 hundred these values. 185 00:09:29,280 --> 00:09:34,410 When I come back from the function call, I'm going to display X and Y, I'm not going to display this 186 00:09:34,410 --> 00:09:37,770 X and this Y, not the ones in the lambda. 187 00:09:37,770 --> 00:09:40,350 So that's going to display the 100 and the 200. 188 00:09:40,740 --> 00:09:41,310 Okay. 189 00:09:41,310 --> 00:09:45,840 So let's do a couple more and then I'll switch over to another video because this one is probably going 190 00:09:45,840 --> 00:09:47,580 to get a little bit wrong in this case. 191 00:09:47,580 --> 00:09:50,240 We are doing default capture by reference. 192 00:09:50,250 --> 00:09:52,950 That means we're capturing everything by reference. 193 00:09:53,520 --> 00:09:56,340 Those are my three variables here X, Y and Z. 194 00:09:56,400 --> 00:09:58,530 I'm capturing everything by reference. 195 00:09:58,530 --> 00:10:02,100 So it's a single ampersand sign in the capture list. 196 00:10:02,730 --> 00:10:04,020 So what does that mean? 197 00:10:04,020 --> 00:10:09,900 That means that I've got an X, Y and Z that are references to the actuals, right? 198 00:10:09,900 --> 00:10:10,980 So that's what we've got. 199 00:10:10,980 --> 00:10:12,510 We're not making copies of these guys. 200 00:10:12,510 --> 00:10:13,800 We've got references. 201 00:10:14,670 --> 00:10:16,530 And we're going to increment each by 100. 202 00:10:16,590 --> 00:10:18,300 And we're going to display them. 203 00:10:18,570 --> 00:10:23,160 So when I call this function, what's going to happen is this guy will become 200. 204 00:10:23,730 --> 00:10:25,830 This guy will become 300. 205 00:10:26,130 --> 00:10:28,470 And Z will become 400. 206 00:10:29,160 --> 00:10:30,960 This function will display them. 207 00:10:30,960 --> 00:10:32,000 And we're in test five. 208 00:10:32,010 --> 00:10:36,300 So you can see right here we're displaying 203 hundred and 400. 209 00:10:36,420 --> 00:10:39,030 Now, when we come back from the function. 210 00:10:40,100 --> 00:10:43,910 We're going to display these guys while those are the same ones we just displayed. 211 00:10:43,910 --> 00:10:47,720 Right, because we actually change the actuals through these references. 212 00:10:47,840 --> 00:10:49,610 So, again, that should be pretty straightforward, I think. 213 00:10:49,820 --> 00:10:52,220 Hopefully everything is starting to come together a little bit. 214 00:10:52,490 --> 00:10:55,430 Once you get past the syntax, I think it's pretty straightforward. 215 00:10:55,820 --> 00:10:56,910 Let's do one more. 216 00:10:56,930 --> 00:10:59,570 And in this case, it's six. 217 00:11:00,230 --> 00:11:01,790 Let me scroll up here again a little bit. 218 00:11:02,090 --> 00:11:04,250 So we're in test six where we're right up here. 219 00:11:04,250 --> 00:11:07,850 And in test six, we've got those three variables again, X, Y and Z. 220 00:11:08,060 --> 00:11:12,710 And now what we're doing, we're capturing by value and we're capturing by reference. 221 00:11:12,710 --> 00:11:13,970 So that's the syntax here. 222 00:11:13,970 --> 00:11:19,880 It says Capture everything by value except y, capture y by reference. 223 00:11:20,330 --> 00:11:25,790 So again, capture everything by value, but capture Y by reference and it's mutable. 224 00:11:25,790 --> 00:11:28,690 So we can change those, the values we're capturing. 225 00:11:28,700 --> 00:11:33,740 So now we've got our X here, we're capturing X by value. 226 00:11:33,750 --> 00:11:35,270 So I'm going to copy the 100. 227 00:11:36,150 --> 00:11:38,310 We're capturing y by reference. 228 00:11:38,310 --> 00:11:40,680 So here's my why it's a reference. 229 00:11:41,310 --> 00:11:43,830 And my z I'm capturing by value. 230 00:11:44,460 --> 00:11:48,690 So my Z is going to be 300 right here, which I've copied. 231 00:11:49,170 --> 00:11:51,810 And then what I'm doing is I'm just going to add 100 to each one of them. 232 00:11:51,810 --> 00:11:53,490 So I'm going to call this LAMDA. 233 00:11:54,150 --> 00:11:57,150 What's going to happen is I'm going to increment X by 100. 234 00:11:57,450 --> 00:11:58,560 That's this guy. 235 00:11:59,310 --> 00:12:01,260 I'm going to increment Y by 100. 236 00:12:01,260 --> 00:12:02,130 That's my reference. 237 00:12:02,130 --> 00:12:03,980 So this guy now becomes 300. 238 00:12:04,810 --> 00:12:08,320 And then I'm going to increment ZE by 100, so this becomes 400. 239 00:12:08,740 --> 00:12:10,310 Then I'm going to display X, Y and Z. 240 00:12:10,330 --> 00:12:13,390 Well, that's 200, 300, 400. 241 00:12:13,390 --> 00:12:15,130 And that's what we see right there. 242 00:12:16,210 --> 00:12:20,450 When we come back from the function call, I'm willing to display X, Y and Z. 243 00:12:20,470 --> 00:12:23,880 Now we're going to display these guys right up here, X, Y and Z. 244 00:12:23,890 --> 00:12:26,800 So it's going to display 103 hundred and 300. 245 00:12:27,250 --> 00:12:28,780 And that's what you get right there. 246 00:12:29,140 --> 00:12:30,280 So I'm going to stop the video here. 247 00:12:30,280 --> 00:12:34,500 And then in the next video, I'll start from Test seven and we'll start right from there. 248 00:12:34,510 --> 00:12:35,620 I'll see you in the next video.