Welcome back.
Let's talk about filter.
Now, based on the name, you should guess what it does.
It filters things for us.
So with map we always got the same number of items back, right?
We have three items and we got back three items.
We just mapped over each item.
With filter, we can sometimes receive less than what we gave it.
We're filtering some of our results.
So let's create a new function instead of multiplied by two.
I want to create a function.
And this function, what are we going to do to the items?
Let's say if items are check if odd.
So we're going to say, hey, are these numbers odd and only return the odd numbers.
So check.
Odd is going to receive an item.
And this item is going to be acted upon.
And once again, we have to return something.
What are we going to return?
Well, the filter function is going to try and receive a true and false value or a Boolean value, whether
it should be filtered or it should not.
So if we want to check if a number is odd, well, we can say item modulo two because modulo two is
going to divide the number by two.
And if it equals zero, then it's even.
Right.
So I can say it doesn't equals zero.
So if the remainder doesn't equal zero, it's going to be odd.
And we just simply return this.
Again, this is going to evaluate into a bullying expression and based on the bullying expression,
filter is going to say, if it's true, I'm going to keep it in the list.
If it's not, I'm going to remove it or not add it to the new list.
So let's check this out instead of map, let's do filter and here we'll do check.
Odd.
Or.
You know what?
I don't like this name.
Let's do.
Only odd.
Like that.
If I run this.
Look at that.
I've filtered and removed the tube because that's an even number.
Again.
Notice here how I'm not calling the function because filter accepts a function or what we call function
signature that is.
Hey, just tell me where I can find what memory space I should go to for what action to take.
And it's going to say, Hey, give me the data and I'm going to go and run this function on each item.
So when we go to one, it's going to run this function with one as the argument.
One divided by two.
Well, that definitely doesn't give us a remainder of zero.
So that's going to return.
True.
And that means I'm going to add it to the new list that we create when we get to two, though, because
this is going to return false.
It's not going to add it to the list.
Again if let's say we have a list of users.
In that case, we might want to filter out based on users, starting with the name A.
We can use the filter function to filter through all the usernames.
And only have a new list with the usernames with letters, starting with a.
Another very useful function.
And as you can see, we kept everything pure.
It creates a new list for us.
It doesn't modify the original list.
So we're keeping that functional paradigm in mind.
And once again, the beauty with both map and filter is look how simple the functions become.
Everything is just nice and simple, and mind you, they can get a little bit more complicated.
But all we need to know is, Hey, what should I do to the item and what should I return?
That's the only thing that they're concerned about.
Instead of having these massive, massive functions that try to do everything.
All right.
Let's talk about zip next.