loading...

30 Days of Python πŸ‘¨β€πŸ’» - Day 11 - Functional Programming Basics

arindamdawn profile image Arindam Dawn Updated on ・4 min read

30-days-of-python (30 Part Series)

1) 30 Days of Python πŸ‘¨β€πŸ’» - Day 1 - Introduction 2) 30 Days of Python πŸ‘¨β€πŸ’» - Day 2 - Data Types I 3 ... 28 3) 30 Days of Python πŸ‘¨β€πŸ’» - Day 3 - Data Types II 4) 30 Days of Python πŸ‘¨β€πŸ’» - Day 4 - Data Types III 5) 30 Days of Python πŸ‘¨β€πŸ’» - Day 5 - Conditions & Loops I 6) 30 Days of Python πŸ‘¨β€πŸ’» - Day 6 - Loops II & Functions 7) 30 Days of Python πŸ‘¨β€πŸ’» - Day 7 - Developer Environment 8) 30 Days of Python πŸ‘¨β€πŸ’» - Day 8 - OOP Basics 9) 30 Days of Python πŸ‘¨β€πŸ’» - Day 9 - OOP Pillars 10) 30 Days of Python πŸ‘¨β€πŸ’» - Day 10 - OOP Missing Pieces 11) 30 Days of Python πŸ‘¨β€πŸ’» - Day 11 - Functional Programming Basics 12) 30 Days of Python πŸ‘¨β€πŸ’» - Day 12 - Lambda Expressions & Comprehensions 13) 30 Days of Python πŸ‘¨β€πŸ’» - Day 13 - Decorators 14) 30 Days of Python πŸ‘¨β€πŸ’» - Day 14 - Error Handling 15) 30 Days of Python πŸ‘¨β€πŸ’» - Day 15 - Generators 16) 30 Days of Python πŸ‘¨β€πŸ’» - Day 16 - Module Basics 17) 30 Days of Python πŸ‘¨β€πŸ’» - Day 17 - External Modules 18) 30 Days of Python πŸ‘¨β€πŸ’» - Day 18 - File I/O 19) 30 Days of Python πŸ‘¨β€πŸ’» - Day 19 - Regular Expressions 20) 30 Days of Python πŸ‘¨β€πŸ’» - Day 20 - Debugging and Testing 21) 30 Days of Python πŸ‘¨β€πŸ’» - Day 21 - Scripting Basics 22) 30 Days of Python πŸ‘¨β€πŸ’» - Day 22 - Scripting Extras 23) 30 Days of Python πŸ‘¨β€πŸ’» - Day 23 - Web Scraping 24) 30 Days of Python πŸ‘¨β€πŸ’» - Day 24 - Web Development Basics 25) 30 Days of Python πŸ‘¨β€πŸ’» - Day 25 - Web Development Extras 26) 30 Days of Python πŸ‘¨β€πŸ’» - Day 26 - Machine Learning Basics 27) 30 Days of Python πŸ‘¨β€πŸ’» - Day 27 - ML & Data Science I 28) 30 Days of Python πŸ‘¨β€πŸ’» - Day 28- ML & Data Science II 29) 30 Days of Python πŸ‘¨β€πŸ’» - Day 29 - Automation Testing 30) 30 Days of Python πŸ‘¨β€πŸ’» - Day 30 - Free Python Resources

Both Object-Oriented Programming and Functional paradigms of programming operate on the same principle - Separation of Concerns. While Object-Oriented Programming does this by combining properties and their actions in a class, functional programming keeps properties and their actions separate and use functions to perform actions on objects.

Today, I explored the functional programming concepts as applied to Python and looked at how we can implement functional programming techniques to organize Python code.

In the developer universe, there is often a debate between which style of programming paradigm is better. While many have strong opinions about either writing pure Object Oriented code or pure functional code. The more pragmatic approach is to understand the benefits of both paradigms along with their caveats and embrace the best of both worlds whenever necessary.

Here are some of the important concepts of functional programming and their implementation in Python.

Pure Functions

Pure functions are the heart and soul of functional programming just like classes and objects are of Object-Oriented programming.

A pure function is a function that follows these two rules:

  • If provided the same input, it will return the same output always.
  • It does not result in any side-effects.

Side-Effects generally mean changing data outside the scope of the function such as printing to console, doing a network request, making a database change, accessing a global variable etc.

def doubler(num):
  '''
  Accepts a number and multiplies it by 2
  '''
  return num * 2

print(doubler(5))
def emoji_appender(list, emoji):
  '''
  Accepts a list and a emoji and 
  appends to every item of list
  '''
  new_list = []
  for item in list:
    new_list.append(str(item) + emoji)
  return new_list

print(emoji_appender([1,2,3], 'πŸ˜€')) # ['1πŸ˜€', '2πŸ˜€', '3πŸ˜€']
print(emoji_appender(['alpha','beta','gamma'], 'πŸ˜€')) 
# ['alphaπŸ˜€', 'betaπŸ˜€', 'gammaπŸ˜€']

What are the benefits of Pure Functions?

A pure function should ideally perform only a single, specific action. Since pure functions always return the same output, provided the same input, they are very easy to test as they are predictable. This predictability feature always allows several pure functions to be run in parallel as they don't have in side-effects. It makes code easier to read and understand.

Some important built-in functions

Python comes with some built-in set of functions that allows code to be written in a functional style or in a more declarative way. These functions being pure, do not modify the input data either create any side-effects.

  • map()

map function takes a function as the first argument that does some kind of action and an iterable as the second argument. It basically loops over each item of the iterable and applies the passed function.

numbers = [1,2,3,4,5]
def multiply_by5(num):
  return num * 5

result = map(multiply_by5, numbers)

print(result) # <map object at 0x7f572dcb7730> (Memory location of the map object)
print(list(result)) # [5, 10, 15, 20, 25] (to get the updated list)
print(numbers) # [1,2,3,4,5] (Unmodified)`

map function returns a reference to the memory location of the map object. To get the resultant data, it needs to be passed as an argument to the list function. An important thing to note about the map function is the input and output of the function has the same length. map does not modify the input iterable.

  • filter()

filter, as the name suggests filters an input iterable data based on the passed function

color_to_remove = 'red'

colors = ['blue', 'green', 'black', 'red']

def remove_color(color):
  return color != color_to_remove

result = filter(remove_color, colors)

print(list(result)) # ['blue', 'green', 'black']
print(colors) # ['blue', 'green', 'black', 'red'] (Unmodified)

just like map, filter function returns the reference to the address in the memory where the filter object is stored and to get the actual result it needs to be passed to the list function. The length of the output can be equal or less than that of the input depending on the condition. filter also does not change or mutate the input data

  • zip()

The zip built-in function accepts multiple iterables and groups or zips them as tuples. This kind of feature can be very helpful in scenarios when different user data are stored in different columns in a database and they need to combined together based on their relationship.

emails = ['alan@gmail.com', 'ross@gmail.com']
usernames = ['alan', 'ross']

users = list(zip(emails,usernames))
print(users) # [('alan@gmail.com', 'alan'), ('ross@gmail.com', 'ross')]
print(emails) # ['alan@gmail.com', 'ross@gmail.com'] (Unmodified)
print(usernames) # ['alan', 'ross']
  • reduce()

reduce is a bit different from the other functions mentioned above. reduce is not a part of the built-in python functions. It is a part of a package or a toolbelt that gets downloaded along with the python interpreter and packages. So it needs to be imported from the functools module. Will explore more about modules later.

reduce is a bit tricky to understand. However, borrowing from the JavaScript universe, where the array method reduce also does the same thing. It keeps a track of the resultant value in the accumulator.

reduce accepts a function and an iterable as required parameters and an optional initializer which is set to 0 by default.

reduce can be thought of as a way to reduce or merge iterable values into a single value.

A great article to understand the reduce function in depth.

from functools import reduce

numbers = [1,2,3,4]
def accumulator(acc, curr):
  return acc + curr

sum = reduce(accumulator, numbers, 0)
print(sum) # 10

That's all for today. The core concepts have been explored mostly, I believe the concepts of functional programming will re-appear in the further advanced sections of Python. I would like to go through few other remaining functional programming terminologies tomorrow and share the same in a lucid way. I am getting more and more excited as I am unlocking new concepts and exploring new territories in Python. Hope you are equally excited and curios to follow along.

Have a great one!

30-days-of-python (30 Part Series)

1) 30 Days of Python πŸ‘¨β€πŸ’» - Day 1 - Introduction 2) 30 Days of Python πŸ‘¨β€πŸ’» - Day 2 - Data Types I 3 ... 28 3) 30 Days of Python πŸ‘¨β€πŸ’» - Day 3 - Data Types II 4) 30 Days of Python πŸ‘¨β€πŸ’» - Day 4 - Data Types III 5) 30 Days of Python πŸ‘¨β€πŸ’» - Day 5 - Conditions & Loops I 6) 30 Days of Python πŸ‘¨β€πŸ’» - Day 6 - Loops II & Functions 7) 30 Days of Python πŸ‘¨β€πŸ’» - Day 7 - Developer Environment 8) 30 Days of Python πŸ‘¨β€πŸ’» - Day 8 - OOP Basics 9) 30 Days of Python πŸ‘¨β€πŸ’» - Day 9 - OOP Pillars 10) 30 Days of Python πŸ‘¨β€πŸ’» - Day 10 - OOP Missing Pieces 11) 30 Days of Python πŸ‘¨β€πŸ’» - Day 11 - Functional Programming Basics 12) 30 Days of Python πŸ‘¨β€πŸ’» - Day 12 - Lambda Expressions & Comprehensions 13) 30 Days of Python πŸ‘¨β€πŸ’» - Day 13 - Decorators 14) 30 Days of Python πŸ‘¨β€πŸ’» - Day 14 - Error Handling 15) 30 Days of Python πŸ‘¨β€πŸ’» - Day 15 - Generators 16) 30 Days of Python πŸ‘¨β€πŸ’» - Day 16 - Module Basics 17) 30 Days of Python πŸ‘¨β€πŸ’» - Day 17 - External Modules 18) 30 Days of Python πŸ‘¨β€πŸ’» - Day 18 - File I/O 19) 30 Days of Python πŸ‘¨β€πŸ’» - Day 19 - Regular Expressions 20) 30 Days of Python πŸ‘¨β€πŸ’» - Day 20 - Debugging and Testing 21) 30 Days of Python πŸ‘¨β€πŸ’» - Day 21 - Scripting Basics 22) 30 Days of Python πŸ‘¨β€πŸ’» - Day 22 - Scripting Extras 23) 30 Days of Python πŸ‘¨β€πŸ’» - Day 23 - Web Scraping 24) 30 Days of Python πŸ‘¨β€πŸ’» - Day 24 - Web Development Basics 25) 30 Days of Python πŸ‘¨β€πŸ’» - Day 25 - Web Development Extras 26) 30 Days of Python πŸ‘¨β€πŸ’» - Day 26 - Machine Learning Basics 27) 30 Days of Python πŸ‘¨β€πŸ’» - Day 27 - ML & Data Science I 28) 30 Days of Python πŸ‘¨β€πŸ’» - Day 28- ML & Data Science II 29) 30 Days of Python πŸ‘¨β€πŸ’» - Day 29 - Automation Testing 30) 30 Days of Python πŸ‘¨β€πŸ’» - Day 30 - Free Python Resources

Posted on by:

arindamdawn profile

Arindam Dawn

@arindamdawn

Software Engineer who loves building user interfaces. In the quest to learn, unlearn and re-learn things.

Discussion

markdown guide