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 multiply by two.
I want to create a function in 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 what 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 on well we can say item modulo 2 because modulo 2 is going to
divide the number by two.
And if it equals zero then it's even right.
So I can say doesn't equal 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 boolean expression and based on the boolean 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 a filter.
And here we'll do check out or you know what I don't like this name.
Let's do only odd
if I run this look at that I've filtered and remove the too 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 a 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 0.
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 2 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 user starting
with the name A we can use the filter function to filter through all the user names and only have a
new list with the user names 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 about all we need to know is Hey what should
I do to the item and why should I return.
That's the only thing that they're concerned about instead of having these massive massive functions
that tried to do everything.
All right let's talk about zip next.