Welcome back.
I want to talk to you about some useful functions that we get that actually allow us to think in a functional
programming paradigm the name of these functions are map filter zip and reduce.
And these are very very useful very common functions that you write a lot when you're a python program.
So let's explore them first one we're going to start with map and map actually allows us to simplify
the code that we have here of multiply by two you see with map I have it available as a function as
a built in function and we see here that we give the first parameter a function and then the second
parameter is an honorable a good way to think about it is we want to take some sort of action here.
So a function and then the data that we want to take action upon.
So let's say in our case 1 2 3.
So what action do we want to take on 1 2 3.
Well we want to multiply by two.
So let's say multiply.
Now I have to move this down because we have to define the function first
and run it like this.
So instead of printing this let's do
like this if I run it I get a map object at this memory location and map automatically gives us this
object that it has created in this memory so in order for us to actually view it we have to turn it
into a list like this so if I run this it says on line for into object is not ignorable.
Well it's because with map we no longer need to do this creation of a list then appending to a list.
You see the neat thing about Knapp is that I give it some data and then this data gets acted upon by
this and all we need to do with the map function is to have a function that returns this part item times
2.
So instead of having all of this code all we need to do is say return item times to so let's change
this parameter to item and look at the code a little bit more in detail.
So with the map I say hey I want you to run the function multiply by 2.
Notice here that I don't even have the brackets because I don't need to call it map does it for me.
Map just says hey just give me the function.
I'll take care of the rest.
So I'm not calling the function here.
The map is going to call the function for me.
I'm just saying hey when I call map like this I want you to use this function at this memory address
and use this data.
Remember our functional programming paradigm.
We have data that gets acted upon.
So we separate those two out.
Now this function is going to take each one of the items in our era bowls in our case first time around
item will be one then two then three and then all we need to do is to return what effect we want to
have on that item in our case it's two.
So by doing this map automatically runs this function for us and loops through all the items in the
interval and returns for us a new map object that we're going to convert into a list.
If I run this I get 2 4 6.
Now here's the neat part if I do let's say my list and I say 1 2 3 and I change my list over here like
this.
And at the end also print my list if I click run you'll see that my list hasn't changed.
Remember a pure function is a function that doesn't affect the outside world.
And this way map allows us to create a whole new list that doesn't modify my list so my list is immutable
right.
We don't change it and all it does is not function takes care of it and returns a new list for us but
we don't affect the outside world.
There's no side effects in this function.
We're simply just multiplying item to item by two but creating a new list out of this and every time
we give it the same input it's going to result in the same output very very cool and map is extremely
useful because anytime we have something that we can iterate over and we want to change let's say we
have emails or usernames and we need to update the user names to let's say a b all capital letters in
that case we can iterate and using the math function change it all the user names in our list to let's
say capital letters map is going to be one of those functions that you use a lot in your programming
career and I hope you can see here how clean and nice it is again using functional programming paradigms
let's move on to the next one.
Let's talk about filter next.