1 00:00:05,290 --> 00:00:10,180 Now that we've seen how to implement both stateless and stateful lambdas, let's take a look at more 2 00:00:10,180 --> 00:00:11,230 ways to use them. 3 00:00:11,970 --> 00:00:17,040 The most predominant use of lambdas is in conjunction with the standard template algorithms library 4 00:00:17,070 --> 00:00:19,200 as either a predicate or operator. 5 00:00:19,410 --> 00:00:24,090 Remember, a predicate is just a function that takes several arguments and returns a boolean. 6 00:00:24,540 --> 00:00:30,060 Alternatively, an operator is a function that takes several arguments and applies some operation to 7 00:00:30,060 --> 00:00:30,420 them. 8 00:00:31,640 --> 00:00:36,560 When using lambdas with the SQL Algorithms Library, you'll often hear them referred to as either urinary 9 00:00:36,560 --> 00:00:39,980 binary predicates or urinary binary operations. 10 00:00:40,310 --> 00:00:45,650 This simply refers to whether the lambda will act as a predicate or as an operator, and how many parameters 11 00:00:45,650 --> 00:00:46,700 the lambda requires. 12 00:00:46,700 --> 00:00:49,100 Urinary meaning one in binary meaning to. 13 00:00:49,740 --> 00:00:53,670 We've already seen lots of examples that use lambdas with the SQL algorithms. 14 00:00:53,670 --> 00:00:58,920 We've seen examples in this section and we also saw examples in the SQL section of the course. 15 00:00:59,580 --> 00:01:04,050 Now that we've got the jargon down, let's take a look at a few more examples of how to use lambdas 16 00:01:04,050 --> 00:01:06,090 with a few more SQL algorithms. 17 00:01:11,740 --> 00:01:12,070 Okay. 18 00:01:12,070 --> 00:01:14,350 So then let's go through some examples in the ID. 19 00:01:14,380 --> 00:01:21,040 You can see here that I'm in the Section 21 workspace and I'm in the SQL Underscore Lambdas Project. 20 00:01:21,730 --> 00:01:23,260 I've already gone ahead and built and run it. 21 00:01:23,260 --> 00:01:29,230 You can see the run out here on the right side and let's go over the includes I o stream vector algorithm 22 00:01:29,230 --> 00:01:31,090 and I'm also including numeric. 23 00:01:31,090 --> 00:01:35,620 This is for a function called iota, which is pretty handy little function that I haven't covered. 24 00:01:35,620 --> 00:01:37,870 So I thought I would throw it in just so you could learn it. 25 00:01:37,870 --> 00:01:41,440 The algorithms that I'm going to be using in here are pretty straightforward ones. 26 00:01:41,440 --> 00:01:42,670 There are a lot more of them. 27 00:01:42,670 --> 00:01:47,170 As I said, please see your CPP reference website. 28 00:01:47,200 --> 00:01:50,380 It's got documentation on all the SQL algorithms. 29 00:01:50,380 --> 00:01:54,130 Again, this video is not about the algorithms, it's about using lambdas with them. 30 00:01:54,130 --> 00:01:55,330 Let's go over some examples. 31 00:01:55,330 --> 00:02:00,310 I've got, I think seven tests that will run and I'll walk through them just like I have before the 32 00:02:00,310 --> 00:02:02,050 first one we've already seen before. 33 00:02:02,050 --> 00:02:06,460 It's for each and I've included this as well as sort in this video. 34 00:02:06,550 --> 00:02:10,120 In case somebody jumps straight into this video and they haven't seen them before, they can see them 35 00:02:10,120 --> 00:02:12,190 here for the first time and I'll walk through them again. 36 00:02:12,190 --> 00:02:14,110 Okay, so let's start with test one. 37 00:02:14,110 --> 00:02:17,860 You can see test one's output is right up here in test one. 38 00:02:17,860 --> 00:02:20,560 What we've got is we're going over the for each algorithm. 39 00:02:20,560 --> 00:02:24,700 Now I've already covered the for each algorithm a couple of times in this section as well as in the 40 00:02:24,700 --> 00:02:25,300 course. 41 00:02:25,300 --> 00:02:29,800 It's a very handy algorithm that lets you iterate over a container in the standard template library. 42 00:02:29,800 --> 00:02:31,540 So it's not modifying. 43 00:02:31,540 --> 00:02:33,370 So we're not going to modify anything in the container. 44 00:02:33,370 --> 00:02:36,160 It just basically processes each element, if you will. 45 00:02:36,430 --> 00:02:42,640 In this case, I've got nums, which is a vector of five integers, and I've initialized it to ten, 46 00:02:42,640 --> 00:02:44,140 20, 30, 40, 50. 47 00:02:44,140 --> 00:02:48,310 And then what we're going to do is we're going to call for each from the beginning to the end. 48 00:02:48,310 --> 00:02:50,380 So we're going to cover the entire container. 49 00:02:50,380 --> 00:02:54,760 We don't have to we can only loop once or twice or three or four elements, whatever we want. 50 00:02:54,760 --> 00:02:58,150 But in this case, I'm iterating over the entire vector. 51 00:02:58,150 --> 00:03:05,110 And what's going to happen is for each element in that vector, it will be passed into this lambda and 52 00:03:05,350 --> 00:03:06,310 we're not doing that right. 53 00:03:06,310 --> 00:03:07,930 The for each algorithm is doing that. 54 00:03:07,930 --> 00:03:12,820 And then we just use this lambda as an operator here, not a predicate, it's just a regular operator. 55 00:03:12,820 --> 00:03:14,020 We just do some stuff. 56 00:03:14,020 --> 00:03:15,910 So in this case we're just displaying. 57 00:03:16,180 --> 00:03:18,520 So it's going to iterate past those guys in notice. 58 00:03:18,520 --> 00:03:19,600 There's no capture. 59 00:03:19,600 --> 00:03:25,120 We only have that one parameter which we need because for each is giving it to us and it's just going 60 00:03:25,120 --> 00:03:27,190 to display ten, 20, 30, 40, 50. 61 00:03:27,190 --> 00:03:32,560 So this is a really, really simple example of using a lambda with one of the algorithms in the standard 62 00:03:32,560 --> 00:03:35,530 template library, a very handy algorithm as well. 63 00:03:35,560 --> 00:03:38,170 I'm using, it's here, but this can be a container of anything. 64 00:03:38,710 --> 00:03:42,820 OC So let's take a look at a different one that we probably haven't seen before. 65 00:03:43,060 --> 00:03:50,650 This one is called is permutation, this one is called is permutation and it's also non modifying. 66 00:03:50,650 --> 00:03:56,500 In this case I'm using triangles, so it tests whether two triangles are equivalent and we defining 67 00:03:56,500 --> 00:04:01,390 a triangle being equivalent as a triangle is a vector of three points in any order. 68 00:04:01,390 --> 00:04:05,680 So regardless of the order, if two triangles contain those same three points, they're equivalent. 69 00:04:05,680 --> 00:04:07,480 So pretty simple definition. 70 00:04:07,480 --> 00:04:10,630 So before we use the algorithm, let's take a look at what we've done. 71 00:04:10,630 --> 00:04:12,490 Let me scroll up just a little bit here. 72 00:04:12,490 --> 00:04:16,329 So here's test two and you can see test choose output right over here. 73 00:04:17,290 --> 00:04:21,880 And I've got a structure that I've defined called Point, which is just an X Y attribute. 74 00:04:22,210 --> 00:04:26,110 And then I'm going to create four points .1. to .3.4. 75 00:04:26,440 --> 00:04:29,020 And based on this points, I'm going to create a triangle. 76 00:04:29,020 --> 00:04:39,280 So a triangle is a vector of points, three points triangle one is 0.1.2.3 triangle two is 0.2.3.1. 77 00:04:39,280 --> 00:04:42,160 And then triangle four includes that 0.4 here. 78 00:04:42,160 --> 00:04:48,300 So according to our definition, these two are equivalent because we can change the order, right? 79 00:04:48,400 --> 00:04:50,560 I think everybody understands what a permutation is. 80 00:04:50,560 --> 00:04:53,260 We're just kind of mixing these things up in different orders. 81 00:04:53,260 --> 00:04:58,000 Eventually we can commute one of these and get one of those because we've got one, two, three in both 82 00:04:58,000 --> 00:04:58,570 of them. 83 00:04:58,570 --> 00:05:04,180 However, triangle three is not equivalent because that 0.4 doesn't exist in either triangle one or 84 00:05:04,180 --> 00:05:04,990 triangle two. 85 00:05:04,990 --> 00:05:07,030 And again, the important thing is the lambda here. 86 00:05:07,030 --> 00:05:10,570 So I'm going to call stood is permutation. 87 00:05:10,660 --> 00:05:16,420 I'm going to pass in triangle one, begin triangle one, and that's going from the beginning to the 88 00:05:16,420 --> 00:05:17,530 end of this vector. 89 00:05:17,530 --> 00:05:20,050 And then I'm passing in triangle to begin. 90 00:05:20,440 --> 00:05:22,570 Let me spread this out just a little so you can see it. 91 00:05:22,570 --> 00:05:27,460 So we are also expecting two points now the stood is permutation function. 92 00:05:27,610 --> 00:05:28,750 That's what it's doing. 93 00:05:28,750 --> 00:05:33,850 It's passing those two points into us and it's up to us to compare them however we like. 94 00:05:33,850 --> 00:05:39,190 So we're getting the left hand side and the right hand side and it's using the operator equal to compare. 95 00:05:39,190 --> 00:05:44,350 So what's going to happen is I'm going to compare my left hand sides X to my right hand side sex and 96 00:05:44,350 --> 00:05:46,840 the left hand sides Y to the right hand side y. 97 00:05:46,840 --> 00:05:48,550 So our points are equivalent. 98 00:05:48,550 --> 00:05:55,870 So when we call is permutation and we get a permutation that matches one of those, we've got an equivalence 99 00:05:55,870 --> 00:05:57,130 and that's pretty much it. 100 00:05:57,130 --> 00:06:01,480 So we're using that in the if statement and if they're equivalent, we're just displaying that. 101 00:06:01,480 --> 00:06:03,190 If they're not equivalent, we're displaying that. 102 00:06:03,190 --> 00:06:08,620 And you can see from this test case right here, that triangle one and triangle two are equivalent. 103 00:06:08,620 --> 00:06:11,110 And that's exactly what's being displayed right over here. 104 00:06:11,290 --> 00:06:14,740 Let me move this back because I think everybody sees what's going on now. 105 00:06:15,070 --> 00:06:20,070 In the other example down here, I'm using triangle one and triangle three. 106 00:06:20,080 --> 00:06:22,600 Now, remember, a triangle three has that fourth point in there. 107 00:06:22,600 --> 00:06:24,490 So it's not going to be equivalent. 108 00:06:24,490 --> 00:06:26,350 And that's exactly what happens right here. 109 00:06:26,410 --> 00:06:27,370 We've got triangle one. 110 00:06:27,370 --> 00:06:28,690 Triangle three is not equivalent. 111 00:06:28,690 --> 00:06:32,470 So again, just another example of passing in a lambda here. 112 00:06:32,470 --> 00:06:37,480 This lambda does not capture and it expects two parameters in this case. 113 00:06:37,480 --> 00:06:40,960 This is a predicate, lambda, because it's returning a true false value. 114 00:06:40,990 --> 00:06:41,260 All right. 115 00:06:41,260 --> 00:06:43,540 So let's move on to test three. 116 00:06:43,570 --> 00:06:46,630 In test three, I'm using the transform algorithm. 117 00:06:46,630 --> 00:06:48,790 This is a modifying operation. 118 00:06:48,790 --> 00:06:54,520 And if you recall in the previous examples, we created that Lambda Bonus where we awarded bonus points 119 00:06:54,520 --> 00:06:56,140 to test scores and so forth. 120 00:06:56,140 --> 00:06:58,990 Well, we're going to do the same thing, except we're going to use to transform. 121 00:06:58,990 --> 00:07:00,220 So let's do it. 122 00:07:00,250 --> 00:07:07,810 We've got test scores and it is a vector of integers and those are my test scores 93, 88, 75, 68, 123 00:07:07,810 --> 00:07:08,710 65. 124 00:07:08,710 --> 00:07:14,410 And these are the bonus points that I want to add to each one of those test scores right away. 125 00:07:14,410 --> 00:07:16,180 You know, we've got a local variable here. 126 00:07:16,180 --> 00:07:18,820 If we're going to use that in the lambda, we need to capture it. 127 00:07:19,330 --> 00:07:21,670 So that's what we're going to do in this example. 128 00:07:21,670 --> 00:07:25,510 We are going to transform that container. 129 00:07:25,930 --> 00:07:28,300 Test scores begin, test scores don't end. 130 00:07:28,300 --> 00:07:33,790 So we're doing the entire container and we're going to start writing back the information at test scores 131 00:07:34,120 --> 00:07:34,690 again. 132 00:07:34,720 --> 00:07:38,800 So we're going to start from here and we're going to start updating from here as well. 133 00:07:38,830 --> 00:07:41,380 We're capturing bonus points by value. 134 00:07:41,380 --> 00:07:43,150 That's this guy right here. 135 00:07:43,150 --> 00:07:46,060 So we're going to capture those bonus points by value. 136 00:07:46,090 --> 00:07:52,300 And then it stood transform is going to pass into the lambda each one of those scores as it iterates 137 00:07:52,300 --> 00:07:52,870 over it. 138 00:07:53,110 --> 00:07:58,120 So those scores will be passed into here 93, then 88 and 75 and so forth. 139 00:07:58,120 --> 00:08:03,790 So what we're doing now is we're returning score plus equals five in this case. 140 00:08:04,180 --> 00:08:09,370 So what we're going to do is we're going to increment each one of those test scores by five points. 141 00:08:09,520 --> 00:08:11,890 So let's take a look at the output right over here. 142 00:08:11,890 --> 00:08:12,490 Right here. 143 00:08:12,490 --> 00:08:19,930 You can see I'm in test three and you can see right there that we've gone from 93 to 98, from 88 to 144 00:08:19,930 --> 00:08:21,520 93 and so forth. 145 00:08:21,520 --> 00:08:24,910 So we've added five points to each one of those scores. 146 00:08:24,910 --> 00:08:27,430 I just used a range based for loop here. 147 00:08:27,430 --> 00:08:29,560 I could have used the for each algorithm as well. 148 00:08:29,560 --> 00:08:31,420 I'll leave that up to you guys to decide. 149 00:08:32,350 --> 00:08:34,720 Okay, so let's take a look at test four. 150 00:08:34,929 --> 00:08:39,730 I've got a vector of integers called nums and you can see I've initialized it here to one, two, three, 151 00:08:39,730 --> 00:08:40,570 four, five. 152 00:08:40,570 --> 00:08:45,580 Now what we're going to do is we're going to remove the even numbers from that vector. 153 00:08:45,610 --> 00:08:48,640 Then once we remove them, we're going to erase them. 154 00:08:49,030 --> 00:08:55,270 So what we're doing here is first we're calling remove if and we're telling remove if that we want to 155 00:08:55,270 --> 00:08:59,170 iterate over the entire container numbers from beginning to end. 156 00:08:59,410 --> 00:09:05,830 And what remove if we'll do is it will pass in each one of those values from that container into this 157 00:09:05,830 --> 00:09:06,460 lambda. 158 00:09:06,790 --> 00:09:10,990 This is a predicate lambda and we're returning true for whatever we want to delete. 159 00:09:10,990 --> 00:09:14,590 So in this case we're returning true for all even numbers. 160 00:09:14,590 --> 00:09:20,800 So two and four are what we want to eventually erase, which right now we're just removing. 161 00:09:20,800 --> 00:09:25,690 What happens is it puts those numbers at the end and sets the end to it. 162 00:09:25,690 --> 00:09:32,770 So now we're telling nums dot erase to erase what it returned and use nums end as the new end. 163 00:09:32,770 --> 00:09:35,020 So it's basically going to chop off those numbers. 164 00:09:35,020 --> 00:09:40,720 And then when we display our updated numbers, you can see right there, one, three and five. 165 00:09:40,720 --> 00:09:43,660 So again, another example of using a predicate, lambda. 166 00:09:43,660 --> 00:09:48,860 This is using the erase remove idiom, which is a very common idiom in the SQL. 167 00:09:48,970 --> 00:09:49,100 Okay. 168 00:09:49,150 --> 00:09:51,730 So let's move on to test five and test five. 169 00:09:51,730 --> 00:09:55,990 I'm using that same person class that I've used throughout this section. 170 00:09:55,990 --> 00:09:57,160 So it's right here. 171 00:09:57,160 --> 00:10:01,630 You can see it's that person class with our overloaded insertion operator. 172 00:10:01,630 --> 00:10:05,500 We've got our name, our age and a bunch of constructors and getters and setters. 173 00:10:05,500 --> 00:10:09,490 So you can refer to the previous videos where I explained this in a little bit more detail. 174 00:10:09,760 --> 00:10:11,860 So what we're going to do now is we're going to sort it now. 175 00:10:11,860 --> 00:10:13,750 You seen this before in the previous video. 176 00:10:13,750 --> 00:10:19,780 I just wanted to include it again, just to be complete because somebody may jump into this video and 177 00:10:19,780 --> 00:10:21,300 not have any idea how it works. 178 00:10:21,300 --> 00:10:24,010 So now, you know, so in this case, really easy. 179 00:10:24,010 --> 00:10:29,410 I've created three person objects, person one, person two in person three, you can see them right 180 00:10:29,410 --> 00:10:29,920 here. 181 00:10:29,920 --> 00:10:31,360 They're emo and curly. 182 00:10:31,900 --> 00:10:36,940 Then I've created a person vector and I've just created it using person one, two and three. 183 00:10:36,940 --> 00:10:39,610 I've initialized it to those person objects. 184 00:10:40,180 --> 00:10:41,380 This is just another way to do it. 185 00:10:41,380 --> 00:10:44,440 I believe I've done it in the other videos, just using Move. 186 00:10:50,860 --> 00:10:55,480 And then we're going to cost sort and we're going to tell students sort that we want to sort that people 187 00:10:55,480 --> 00:10:57,370 vector from begin to end. 188 00:10:57,730 --> 00:11:03,190 And I want it to pass me pairs of persons where that I need to compare. 189 00:11:03,340 --> 00:11:09,670 So it's going to send to me two person objects, one on the left, one on the right, and we're using 190 00:11:09,670 --> 00:11:13,680 the less than operator to compare those two, and we're going to compare by name. 191 00:11:13,690 --> 00:11:19,600 So in this case, you can see we're just comparing by name and we're sorting in increasing alphabetical 192 00:11:19,600 --> 00:11:21,490 order and then we're displaying these. 193 00:11:21,490 --> 00:11:25,780 So what's going to happen is right here, Curly Larry and Moe CLM, right? 194 00:11:25,780 --> 00:11:27,790 So it's in alphabetical order. 195 00:11:28,030 --> 00:11:35,590 So in this case, if we wanted to display the people in that vector by age and in descending order, 196 00:11:35,590 --> 00:11:40,390 then we would just have to say left hand side get age greater than right hand side get h and it will 197 00:11:40,390 --> 00:11:41,260 display. 198 00:11:42,130 --> 00:11:48,670 Moe, Curly and Larry in that order 30, 25, 18 and descending age, really powerful stuff. 199 00:11:48,670 --> 00:11:53,980 All we're doing is changing that lambda, changing that predicate lambda to do what we need. 200 00:11:53,980 --> 00:11:56,050 It's our functional requirements. 201 00:11:56,260 --> 00:12:01,480 If we have to sort by name ascending or by descending, it's really, really easy to do. 202 00:12:02,890 --> 00:12:03,340 All right. 203 00:12:03,340 --> 00:12:06,270 So let's let's move on to another one that's a little bit different. 204 00:12:06,280 --> 00:12:10,600 In this case, I've written a function and that function is called in between. 205 00:12:11,050 --> 00:12:17,890 That function expects a vector of numbers, integers in this case, and I'm passing that in by const 206 00:12:17,890 --> 00:12:19,540 ref a start value. 207 00:12:19,540 --> 00:12:26,860 In an end value, it's going to decide whether all the numbers in that vector are between that start 208 00:12:26,860 --> 00:12:29,110 value and that end value inclusive. 209 00:12:30,240 --> 00:12:33,270 One to use the stood all of algorithm. 210 00:12:33,270 --> 00:12:38,070 It's a non modifying algorithm and what it does is it tests whether all the elements contained within 211 00:12:38,070 --> 00:12:41,280 the sequence satisfy the condition in the lambda. 212 00:12:41,760 --> 00:12:44,130 So in this case I'm assuming that they don't. 213 00:12:44,130 --> 00:12:47,310 So I'm just initializing this result boolean to false. 214 00:12:47,310 --> 00:12:53,820 And then I'm saying result equals stood all of where do I want to start? 215 00:12:53,820 --> 00:12:59,550 I want to start at nums dot begin and I want to go all the way to nums dot n so again, the entire container 216 00:12:59,850 --> 00:13:01,770 I need start value and end value. 217 00:13:01,770 --> 00:13:03,300 So I need to capture those guys. 218 00:13:03,300 --> 00:13:06,210 And that's what's happening right here in the capture list. 219 00:13:07,000 --> 00:13:11,170 I need to know what they are to be able to use them in my lambda right in here. 220 00:13:12,310 --> 00:13:16,750 And withstood all of is going to do is for each element in that container. 221 00:13:16,750 --> 00:13:18,460 It's going to pass it into this lambda. 222 00:13:18,910 --> 00:13:20,980 And all I'm going to do is I'm going to say return. 223 00:13:21,070 --> 00:13:23,260 The value is between star value and end value. 224 00:13:23,260 --> 00:13:25,060 That's what that logic is saying right here. 225 00:13:25,060 --> 00:13:26,170 I'm going to return result. 226 00:13:26,860 --> 00:13:29,650 So really straightforward, really powerful, too. 227 00:13:29,740 --> 00:13:31,990 We don't have to deal with any of this ourselves. 228 00:13:31,990 --> 00:13:33,600 We just here is test six. 229 00:13:33,610 --> 00:13:39,550 Let me scroll this up a little bit as well so you can see the test six output right up here and here's 230 00:13:39,550 --> 00:13:40,060 test six. 231 00:13:40,060 --> 00:13:44,230 I'm setting student alpha just so I can display true false rather than zero one. 232 00:13:44,590 --> 00:13:49,150 So I'm starting with a vector of ten integers called num. 233 00:13:49,150 --> 00:13:54,520 I'm using perrins here, not curlies, so I'm not initializing the first element of that vector to ten. 234 00:13:54,520 --> 00:14:00,100 I'm creating a vector of ten elements and then I'm using that iota function which is in the numeric 235 00:14:00,100 --> 00:14:00,850 library. 236 00:14:00,850 --> 00:14:06,190 And what it does, it goes from nums dot begin to num start end and I'm providing one. 237 00:14:06,190 --> 00:14:11,200 So basically what it's going to do is it's going to set the values in those ten integers to one, two, 238 00:14:11,200 --> 00:14:13,390 three, four, five all the way to ten. 239 00:14:13,750 --> 00:14:18,970 If I'd have started this at ten and here it would go from ten, 11 to 12 and so forth. 240 00:14:18,970 --> 00:14:24,370 Really handy way assigned values to an existing container in incrementing order. 241 00:14:25,270 --> 00:14:30,940 So I'm displaying that and you can see right there that's exactly what I owed has done one through ten, 242 00:14:31,570 --> 00:14:33,220 so I'm going to call in between. 243 00:14:33,220 --> 00:14:35,800 That's my function that I wrote up here. 244 00:14:35,860 --> 00:14:39,490 I'm going to pass in the vector, I'm going to pass in the start in the end. 245 00:14:39,580 --> 00:14:45,790 And what it's going to do is going to check to see if every element in that container is between 50 246 00:14:45,790 --> 00:14:46,570 and 60. 247 00:14:46,690 --> 00:14:48,220 Well, that's not true, right? 248 00:14:48,220 --> 00:14:53,200 Because the container contains one through ten, so they're not all between 50 and 60. 249 00:14:53,200 --> 00:14:55,300 In fact, none of them are between 50 and 60. 250 00:14:55,300 --> 00:14:57,850 So that's going to return false in this case. 251 00:14:57,850 --> 00:15:00,490 I'm going to check to see if the numbers are between one and ten. 252 00:15:00,490 --> 00:15:02,710 Well, they are each returning. 253 00:15:02,710 --> 00:15:03,340 True. 254 00:15:03,370 --> 00:15:07,780 And in this case, I want to check to see if the numbers are between five and seven. 255 00:15:07,810 --> 00:15:08,710 Of course they're not. 256 00:15:08,710 --> 00:15:10,510 So that returns false as well. 257 00:15:10,690 --> 00:15:11,050 Okay. 258 00:15:11,050 --> 00:15:14,200 So then finally, we're at the last test here, test seven. 259 00:15:14,200 --> 00:15:16,930 And this was a little bit more complicated, but it's pretty cool. 260 00:15:16,930 --> 00:15:23,800 We have a class called Password Validator and it contains an attribute called Restricted Symbol. 261 00:15:23,800 --> 00:15:27,430 And in this case, this is a single character that's the dollar sign. 262 00:15:27,430 --> 00:15:31,000 So basically passwords cannot have dollar signs in them. 263 00:15:31,780 --> 00:15:37,330 Then I've got a method called is valid, so we're going to pass in a password and this method is going 264 00:15:37,330 --> 00:15:43,450 to tell us whether it's a valid well, it is valid if it doesn't contain that one symbol in this example. 265 00:15:43,630 --> 00:15:50,590 So what this is going to do, it's going to check all of the characters in the password and be sure 266 00:15:50,590 --> 00:15:53,410 that none of them are the dollar sign right now. 267 00:15:53,410 --> 00:15:58,540 We could write loops and we could do all kinds of stuff to do this ourselves, but why not use the algorithm? 268 00:15:58,540 --> 00:16:00,460 All of so let's do that. 269 00:16:00,460 --> 00:16:03,400 I'm checking password from begin to end. 270 00:16:03,400 --> 00:16:06,760 Password is a string, so it's basically a container of characters. 271 00:16:07,060 --> 00:16:15,370 And again, I'm using all of I need to capture this object because I need access to that guy right there. 272 00:16:15,370 --> 00:16:16,480 Restricted symbol. 273 00:16:16,480 --> 00:16:19,120 So remember, we're capturing the object here. 274 00:16:19,120 --> 00:16:23,620 The pointer all of will pass in each character of the password into this lambda. 275 00:16:23,620 --> 00:16:30,550 So now we have everything we need and this will return character not equal to restricted symbol. 276 00:16:31,570 --> 00:16:36,280 So this if the character is not equal to the restricted symbol, we've got a valid password. 277 00:16:36,280 --> 00:16:38,700 And that has to happen for all of the characters. 278 00:16:38,710 --> 00:16:41,500 If any of those fail, it's going to return. 279 00:16:41,500 --> 00:16:42,140 False. 280 00:16:42,160 --> 00:16:42,970 Pretty cool. 281 00:16:42,970 --> 00:16:44,500 So let's see how we would use it. 282 00:16:44,500 --> 00:16:47,950 I've got another example down here that I'll go over in a second, but let's take a look at the all 283 00:16:47,950 --> 00:16:50,010 of and how we would use it right there. 284 00:16:50,020 --> 00:16:54,130 So we've got a password we're creating called Hollywood one with a dollar sign again. 285 00:16:54,130 --> 00:16:56,950 So this is an invalid password because of that guy. 286 00:16:58,190 --> 00:17:02,840 Now I'm instantiating my password validator and I'm just simply calling. 287 00:17:02,840 --> 00:17:06,200 PV one is valid and I'm passing the password into it. 288 00:17:06,740 --> 00:17:07,329 That's it. 289 00:17:07,339 --> 00:17:08,300 Simple as that. 290 00:17:08,660 --> 00:17:09,680 It's going to return. 291 00:17:09,680 --> 00:17:11,270 True if the password is valid. 292 00:17:11,270 --> 00:17:13,010 False if it's invalid. 293 00:17:13,040 --> 00:17:18,500 Well, in this case, we know that that password is not valid because it has the dollar sign in it. 294 00:17:19,280 --> 00:17:25,640 If we change the password to just Hollywood one and execute this code, it will display is valid. 295 00:17:27,060 --> 00:17:27,300 Okay. 296 00:17:27,300 --> 00:17:28,410 So hopefully that makes sense. 297 00:17:28,950 --> 00:17:30,180 You can see what's happening. 298 00:17:30,180 --> 00:17:31,230 It's pretty easy. 299 00:17:31,650 --> 00:17:37,410 We've got characters coming in from that password string and we're comparing them to that restricted 300 00:17:37,410 --> 00:17:39,630 symbol we need to capture. 301 00:17:39,900 --> 00:17:44,520 We need to capture the at this point or otherwise, we won't have access to restricted symbol. 302 00:17:45,210 --> 00:17:46,740 Let's take a look at another example. 303 00:17:47,340 --> 00:17:49,080 This is very, very similar. 304 00:17:49,110 --> 00:17:51,060 This is password validator, too. 305 00:17:51,090 --> 00:17:55,530 The big difference here is I don't have a single character of restricted symbols. 306 00:17:55,560 --> 00:17:57,810 I've got a vector of restricted symbols. 307 00:17:57,810 --> 00:18:00,240 So in this case, I've got a vector of characters. 308 00:18:00,240 --> 00:18:03,840 The bang, the dollar and the plus symbol are all restricted. 309 00:18:03,840 --> 00:18:05,670 That would make it an invalid password. 310 00:18:05,910 --> 00:18:09,180 It changes our logic a little bit, but you can see the power here. 311 00:18:09,210 --> 00:18:12,750 I've still got my is valid method that expects that password. 312 00:18:13,020 --> 00:18:17,190 But what's going to happen now is I'm going to use all of and none of together. 313 00:18:17,640 --> 00:18:18,750 Now that's pretty cool. 314 00:18:19,290 --> 00:18:20,530 So let me show you how that works. 315 00:18:20,550 --> 00:18:24,780 So you could walk through this really carefully, but basically what it's doing, it's making sure that 316 00:18:24,780 --> 00:18:28,890 all of the characters in the password are not restricted. 317 00:18:28,890 --> 00:18:32,630 And in order to do that, we're using stood all of and stood none of. 318 00:18:32,670 --> 00:18:38,520 So again, what we're doing is we're making sure that all of the characters in the password have none 319 00:18:38,520 --> 00:18:40,260 of the restricted symbols in them. 320 00:18:40,260 --> 00:18:45,870 And if we run this example, we've got C++ rocks that has a bunch of illegal characters in it, right? 321 00:18:45,880 --> 00:18:47,360 It's got the plus and the bang. 322 00:18:47,370 --> 00:18:52,790 So we're initializing PV two and we're checking whether it's valid password or not. 323 00:18:52,800 --> 00:18:56,490 Well, it's not valid because it's got the pluses and the bang. 324 00:18:57,310 --> 00:18:59,190 In this case, we've got a single bang. 325 00:18:59,200 --> 00:19:00,700 That's also not valid. 326 00:19:01,210 --> 00:19:04,430 And then in this case, we have none of those characters. 327 00:19:04,450 --> 00:19:06,220 Therefore, the password is valid. 328 00:19:07,840 --> 00:19:13,720 In this video, we've seen some common SQL algorithms and how to use lambdas with them, but it's important 329 00:19:13,720 --> 00:19:16,150 to note that this in no way covers them all. 330 00:19:16,450 --> 00:19:21,820 There are many, many algorithms in the SQL Algorithms Library that use lambdas, and as we saw with 331 00:19:21,820 --> 00:19:26,620 the class password validator too, they can even be used in conjunction with one another. 332 00:19:27,190 --> 00:19:31,270 It would be impossible to cover them all, but hopefully this gives you a good understanding of why 333 00:19:31,270 --> 00:19:34,060 lambdas are so important and their patterns of use. 334 00:19:34,690 --> 00:19:36,130 Learn the SQL algorithms. 335 00:19:36,130 --> 00:19:37,690 They'll become your best friends. 336 00:19:37,720 --> 00:19:43,180 Also, if you're a competitive programmer, consider using the SQL algorithms before you code your own 337 00:19:43,180 --> 00:19:43,960 solution. 338 00:19:45,010 --> 00:19:45,370 Okay. 339 00:19:45,370 --> 00:19:47,410 So that wraps up this section on Lambdas. 340 00:19:47,410 --> 00:19:49,180 I hope you find this section useful.