در حال بارگذاری ...

مرور توابع لامبدا0:00

Hey there, everybody. Welcome back. And you know what? I hope you're excited because, trigger warning, I'm going to push you just a little beyond your comfort zone in this video. We will talk about lambda functions and refactoring and why and when you might want to make a function you have a little more generic and a little more flexible. So let's get started and see what we can come up with. So we will once again work with our books list. And yeah, here's that filterByAuthor function that we worked on in the last episode.

Duplication Problem Setup0:27

So we will once again work with our books list. And yeah, here's that filterByAuthor function that we worked on in the last episode. And yeah, it gets the job done, but it's not the most flexible thing in the world. And here's what I mean. What if you later decide that you also need to filter according to the release year? How exactly do you do it when author is right there in the name? Well, I'll tell you. What a lot of newcomers do is they will reach for duplication. So they add a second function. All right, let's filterByYear.

So they add a second function. All right, let's filter by the year. And then they substitute where they need to. So check if the releaseYear is now equal to not author, but the year. And I haven't checked this, but I'm pretty sure that would work. But once again, hopefully you can see this is not very practical. What about in the future when you need to filter according to 10 different parameters? Does that mean you're going to have 10 different filter functions? Of course not. We don't want to do that.

Extracting a Variable1:21

Of course not. We don't want to do that. So instead, it sounds like we need to make things just a little more generic and flexible. How exactly do we do that? Well, I'll show you how to get there in a few steps. All right, let's get going. First up, notice how we are looping over our books. And originally you learned that you reference a variable name, like books. But then in the last episode, we swapped that out with this function call. But remember, it's still the same thing.

But then in the last episode, we swapped that out with this function call. But remember, it's still the same thing. I'm not saying loop over the function. I'm saying loop over whatever is returned from the function, which itself is an array. So it's still the same thing. But yet, there's nothing keeping you from swapping this out like this. Maybe this is filteredBooks, and now I will extract a variable. And yet, this is actually the jargon that we would generally use, extract variable. And that means exactly what you just saw. We take a bit of logic, and we extract it into a variable,

Anonymous Functions Intro2:16

And that means exactly what you just saw. We take a bit of logic, and we extract it into a variable, and then we substitute the variable wherever that logic used to be. Exactly like this. And yet, this is still going to work exactly the way it did before. Okay, cool. That's step one. Here's step two. Now, what you see here is what's known as a named function. Yeah, a named function is nothing more than a function that has a name.

Now, what you see here is what's known as a named function. Yeah, a named function is nothing more than a function that has a name. Teacher of the year over here. But yeah, as it turns out, there's nothing that requires that functions be named. So for example, if I were to delete that, well, my editor is squawking here, but not because the syntax is invalid. Let's have a look. Expression is not used anywhere. Yeah, so my editor is saying, okay, you created this function, good deal, but you never used it, so you might want to have a look.

Yeah, so my editor is saying, okay, you created this function, good deal, but you never used it, so you might want to have a look. Notice it didn't say invalid because it's not invalid. All right, interesting. So now we know we can create named functions or anonymous functions. So your next question is, well, what do you do with an anonymous function? And the answer is, well, you could assign it to a variable, or you could even pass it to other functions. Kind of interesting. Okay, let's review that variable option.

Kind of interesting. Okay, let's review that variable option. Filter by author. All right, very interesting. So now I have a variable that is equal to a function call. And notice because I have an assignment here, an expression, that means I need to conclude it with a semicolon, which is a little bit different. Okay, but now if I scroll down, I'm trying to use that function, but my editor is complaining, undefined.

Okay, but now if I scroll down, I'm trying to use that function, but my editor is complaining, undefined. And that makes sense. I don't have a named function anymore. I have a variable name that points to a function. That is not a named function. So let's swap this out with the variable name, like so, and now this should do the trick. So yeah, what we have here is what's known technically, to use jargon, as a lambda function.

So yeah, what we have here is what's known technically, to use jargon, as a lambda function. But yeah, for now, just think of it as an anonymous function. That would be fine. Come back to Firefox, cross your fingers, refresh the page, and yeah, it still works. Okay, so now I think we have the tools and the knowledge we need to move on to our refactor. All right, so let's bring this back to a named function. I'll undo this a few steps.

Generic Key-Value Filter4:30

All right, so let's bring this back to a named function. I'll undo this a few steps. And yeah, let's think about it. If I want this to be more generic, it sounds like I can't have any reference to the author because I could be filtering by a category or a release year instead. So why don't we just call it filter? Next though, maybe I'm filtering books, but maybe I could also be filtering

Next though, maybe I'm filtering books, but maybe I could also be filtering a different array entirely. Why don't we make this more generic and as a result, more flexible? So I'm going to change this to, how about items? Some people go with data. Some people just write array. It doesn't really matter. I just want a generic term that could represent

It doesn't really matter. I just want a generic term that could represent any collection of items. So I will stick with that. Now I have to update the names. And then next, I can't say filtered books because it may not be books. So let's select that. And actually real quick, notice how I was able to create multiple cursors here.

Well, the book name, that will simply be item. And then the last thing we should update is this right here. I can no longer assume that we are filtering by the author and I can't assume that I have access to an author variable. Okay, so how would we do that? Well, it sounds like when I call the function, I need to give it a key like author, and then also a value.

I need to give it a key like author, and then also a value. So this is sort of how I want to use the function. I want to give it my array, I want to give it my key, and then I want to give it the value that the key should match. Okay, let's update our function signature. key, value. All right, now we can update this.

Key, value. All right, now we can update this. Key, value. And yeah, with any luck, if we update this, I think this should probably still do the trick. Let's give it a test. Come back to Firefox, give it a refresh, and good, I did make a mistake. And now the cool thing is, this is quite a bit more flexible.

But yeah, once again, it's not the most flexible thing in the world, because what if I instead wanted to say, give me all of the Books that were released after the year 2000? Well, with our current implementation, that's impossible, it just won't work. So how can we make this more flexible? Well, when we started refactoring this function, we took some of the specifics

This is just a simple explanation with no code.

Passing a Callback8:03

And if that sounds confusing, just come along for the ride, and I think it'll make sense. Okay, so let's try a refactor. And I'm gonna start by writing the code that I wish would work. So we learned about anonymous functions. What if I could just say, well, I'm gonna pass an anonymous function or a lambda, and this function needs to accept the current book. And now if we scroll up,

and this function needs to accept the current book. And now if we scroll up, we just want to reproduce what we have here. Okay, so maybe return, look at the book's release year, and tell me if it equals 1968. All right, and this is actually pretty interesting. So think about it. Now I'm saying, ideally, here's how I want it to work. I'd like to call a function named filter. I want to give it an array.

I'd like to call a function named filter. I want to give it an array. And then I also want to pass a function where I can be in charge of how this comparison is made. And in this case, I'm doing equality. But yeah, like we said earlier, I could just as easily say greaterThanOrEqual 2000. And now on the fly, I've changed this comparison. Give me the books where the release year is greater than or equal to 2000.

and my editor's complaining nonstop. Now let's make it work. So we scroll up, and we're gonna update our function signature. And that's the term we use. The signature is the parameters of the function. And we'll say, give me some items, and then give me some kind of function that I can call. So yeah, we could write it out. Some people will do things like this,

So yeah, we could write it out. Some people will do things like this, or you will often see FN, which is short for function. And I'm gonna stick with this approach because if we do, once we start learning about arrow functions in the future, you'll feel a little more at home. Okay, so let's figure this out. We decided that this check here is now being declared when we invoke the filter function.

We decided that this check here is now being declared when we invoke the filter function. So let's swap that out with our function call. But now, hmm, we said right down here that we need to receive the book, right? So that means when I call the function, I need to pass the book, or in this case, the generic item name. Okay, and I think that looks right. Come back and refresh, and it works.

Using array_filter11:05

We can be done for the day. But no, one final thing. So what we've done here is great, and you should pat yourself on the back. But as it turns out, php provides built-in functions for working with arrays. So if I got rid of this and replaced it with php's version, which is called array_filter, well, as it turns out, the signature for that function

well, as it turns out, the signature for that function is very similar to what we have here. And that means if I come back and refresh, I still get the same thing, but now I get to use what php provides out of the box, which is really cool. So now let's just finish up by saying, how about books author, once again, is Andy Weir. And that all works like it did before.

LambdasExtract Variablearray_filter

دوست دارید گاهی خبرهای Laracasts را ایمیل کنیم؟