[Part 1] Functional Programming in Python
For many years I’ve become accustomed to Procedural and Object Oriented programming. Procedural programming, being the most common paradigm I followed, was quick, easy, and logically made sense when typing out the computational steps line by line. Object Oriented programming, to me, was an indispensable tool I carried when I needed to create complex software, but then one evening — I discovered functional programming. This particular programming paradigm was quite new to me. I’d hear it in programming podcasts, I’d see it’s name in various blogs, and glimpse at the various thumbnails containing a lesson on this paradigm on youtube videos. It was just a couple days ago until I became — enlightened. Today I’d like to share what I’ve learned in the past couple of days on the topic of functional programming using the Python programming language. Are you ready? Alright let’s get started.
Before I begin to enlighten you I’d like to pinpoint a few rules, and theories that need to be followed before attempting to use this programming paradigm.
- When using this programming paradigm we need to keep most of our tasks contained within functions. Most of our computational work will be dealt within functions.
- Most of the tasks that we wish to accomplish will be done by passing the output of one function to another. This can be done either through assigning the output of one function to a variable to be used later, or by the method of chaining functions.
- There should be no side-effects from the functions that we are using. What I mean by this is that we need to avoid changing values that are outside the scope of any of the functions that we may be using in our program.
- Functions are first-class citizens. Functions, like variables, can be used as values which is why they are remarked as “first-class citizens”. Functions can also be passed into other functions (if a particular function requires it as it’s input) and can be returned from other functions.
- Finally, we need to limit our functions to accomplishing one particular task and our functions should also be limited in scope. We do not want to create bulky functions whilst at the same time making a particular function do more than one thing.
Now that we are aware of these points, we’ll move on to learning the various functions that further enhance the use of this programming paradigm. The first function we’ll be going over today, out of the various functions that I’ll be presenting in future blog posts, will be the map function.
Simpler code has less bugs.
Imagine yourself creating an square function. You start typing out your usual single character parameter x and you simply return x**2. Great! You followed one of the rules that we mentioned earlier — create functions with a single task. Moments later your boss shows up, sheds a tear at how productive you are and feels confident in giving you a larger task. He ask’s you now to take an list of data and apply your function to each and every item within that list. Now you might attempt to use a list comprehension in order to accomplish this task, which is perfectly fine, but remember we want to keep our tasks within functions as much as possible. This is where the map function comes in.
# The square function
list_of_numbers = [1,2,3,4,5] # our bosses list of numbers
# The traditional way you were thinking before I entered your conscious through this blog post and told you about the use of the map function (if you hadn't already known about the map function....let's just stick with the story...)
list_of_numbers = [square(number) for number in list_of_numbers]
# The use of the amazing map function
map(square,list_of_numbers) # Yes, it's as simple as that. Look at how short that is. *Sheds tear*
What the map function does is take a function as a parameter and an iterable and then applies the function to each and every member of that iterable. It’s just like using a list comprehension except you don’t have to type out any brackets (seriously, those keys are to the far left of your fingers. Who in the world put them there? (ﾉಥ益ಥ）ﾉ ┻━┻)
I hope you enjoyed this this brief yet captivating blog post (Yes — It was captivating, don’t deny it.). I plan on sharing tidbits of programming paradigms, Python programming tips, and anything I find interesting on my journey to gain knowledge. We will together climb the hills of Mount Closure, swing the vines in the forest of unprecedented pythonic syntax, and cheer in the wake of victory as our glorious programs compile in our terminals with utter beauty. Are you ready for the next blog post? I know I am.