Functional Programming in Python

Functional Programming in Python

Introduction

Functional programming is a programming paradigm that treats computation as the evaluation of mathematical functions, avoiding changes of state and mutable data. The main idea of functional programming is “what to solve”. As there are dozens of programming languages which are written in procedural, object-oriented, functional, scripting, etc. ways. So choosing programming language based on its applications is mostly preferred. For example: C is procedural programming language whereas Java and C# are object-oriented languages. In another way Python and JavaScript are functional programming languages but they also support object-oriented functionality. That is why Objects are First Class Citizens in OOPs(Java) and Functions are First Class Citizens in Python.

Functional Programming Concept

In functional programming languages, functions are treated as First Class Citizens means that we can pass functions as arguments in another function, assign function to the variable, returning one function from another function and lots more that we will be cover later in this post.

Pure Functions

Functions here in functional programming should be pure functions that always should return a same value(deterministic).

Pure Function Properties:

  • It does not access global state variables.
  • It always return the same result, given the same argument. So we say in example trigonometric functions sin(x), cos(x), tan(x), etc. are all pure functions.
  • It do not have “side effects”.
  • It cannot use internal state to determining what result to return.

Those functions which are not pure functions are called impure functions. Impure function may return different values depending upon the input from user.

Apart from above criteria, in functional programming repetition must be done by recursion rather than by iteration.

Let us take simple pure function example:

z = 7

def add(x, y):
    return x + y

Above we can see that the add function does not touch the z variable(global variable). It does not read from z and it does not write to z. It only reads x and y, its inputs and returns the result of adding them together.

That’s what called a pure function. But if the add function did access the variable z value and modify it, then it would be no longer a pure function(impure).

High Order Functions

As we already discussed that Python treats functions and methods as first class citizens, so in Python we can pass one function to another function by just treating one function as object. Thus high order functions are those functions that take other functions as inputs and or outputs.

Examples

Passing one function to another

>>> def div(a, b):
...     return a / b
...
>>> def calc(i, j, div):
...     return div(i, j)
...
>>> div(10, 2)
5.0
>>> calc(10, 2, div)
5.0

In above calc function to calculate the division of two numbers, we pass another function method div which perform division operation along with two arguments. In this way in python we can pass one function to another function.

Assigning function to the variable

>>> def div(a, b):
...     return a / b
...
>>> div(10, 2)
5.0
>>> div_var = div
>>> div_var(10, 2)
5.0

Here we assigned function called div to the variable div_var. So instead of using div, now we can use div_var(a, b).

Returning function from another function

>>> def div(a, b):
...     return a / b
...
>>> def calc(i, j):
...     return div(i, j)
...
>>> calc(10, 2)
5.0

In the function calc we return the another function called div.

Advantages of Functional Programming

  • Faster execution via memorization
  • Parallelization
  • Compiler optimization
  • Lazy evaluation

Leave a Reply