Functional programming in Python

Functional programming in Python

In computer science, functional programming is a programming paradigm where programs are constructed by applying and composing functions. (Wikipedia)

Need for Functional Programming(FP)

Functional Programming provides us with separation of concern where we can separate data and logic separately. Hence, the code becomes clear and easy to understand for a developer. Functional Programming follows the DRY (Do not Repeat Yourself) principle. Code which follows Functional Programming practice is memory-efficient. The codebase which implements Functional Programming will also be easy to extend and maintain.

Pure functions

One of the important concepts in functional programming is the usage of pure functions.
A function is said to be a Pure function if:

  1. Given the same input, the function will always return the same output.

  2. The function must not produce any side effects.

Side effects are things that a function does that affect the outside world, that is they change the state of the program.
Changing the data in a variable, and printing output can be considered as some examples of side effects of a function.

Consider the following simple example:

The function square will always return only the square of a given number and will not change anything in the outside world. These types of functions are also called declarative functions

**Note:
**But, technically it’s not possible to use pure functions everywhere as we may need to change the state of the code.
Although, it’s a good practice to use pure functions in as many places as possible.
And the fact here is that it is highly probable to face bugs and errors that occur in non-pure functions rather than in pure functions.
Python provides us with some useful pure functions which are built-in python.

Pure Functions in python:

1) map()

map() accepts two arguments — a function and an iterable.
Consider the above example of the square function, we can make use of the map() function to quickly apply the function to a given iterable like a list of numbers:

Output:

[4, 36, 100]

The most common and useful use case of the map() function is to receive multiple unknown numbers of input from the user:

Output:

Enter some numbers: 2 5 3 7 4  
Check your numbers: [2, 5, 3, 7, 4]

2) filter()

filter() function filters any given iterable based on the specified function.
Consider the following example where the filter() function filters only the items which satisfy the condition of the given only_even() function.

Output:

[2, 4, 6, 8, 10]

3) zip()

When we need to zip two iterables literally, we use the zip() function.
We can add as many iterables as we want inside a zip() function.
Consider the following example with two iterables:

Output:

[('Iron Man', 'Batman'), ('Spider-Man', 'Superman')]

zip() function binds together the corresponding index items in a tuple and inserts them into a list function (as I specified as the list() function).

4) reduce()

reduce() function is part of a functools module — a standard python library.
Consider the following example, where the reduce() function accumulates the given list values and provides a final output.

Output:

9

Unlike other functions, the reduce() function accepts three arguments.
From the example, you can see that the function accumulate() accepts two arguments acc and item and returns the sum.

Here, the reduce() function passes the last argument 0 as acc at first and the first item from the list, then it accumulates and passes the sum as acc for the next list item and so on until all the list items are accumulated and the result is displayed.

Who Am I?

I’m Aswin Barath, a Software Engineering Nerd who loves building Web Applications, now sharing my knowledge through Blogging during the busy time of my freelancing work life. Here’s the link to all of my craziness categorized by platforms under one place: https://linktr.ee/AswinBarath

Join me to learn Python!

Checkout my Python Programming Series where my mission is to share my knowledge on Python: aswinbarath.hashnode.dev/series/python-prog..

Learn what I know about Python from any of my favourite knowledge sources:

Keep Learning

Now, I guess this is where I say GoodBye👋.
But, hey it’s time for you to start learning with your newfound Knowledge(Power)👨‍💻👩‍💻 .
Good Job that you made it this far 👏👏
Thank you so much for reading my Blog🙂.