Functional Programming in Python

Interest in Functional Programming is currently growing as more developers become aware of the benefits it offers, particularly with respect to concurrency and scale.

For some background on Functional Programming see the paper 'Why Functional Programming Matters: http://www.cse.chalmers.se/~rjmh/Papers/whyfp.pdf

There are several aspects of Python that support a functional programming style.

For example: map, reduce and filter.

Map

map applies a function to all the items in an input sequence.

map(function, sequence)

For example:

sequence = [1, 2, 3, 4, 5]

def square(x):
    return x**2

result = list(map(square, sequence))
print(result)

Run the code below to see the output.


In [ ]:
sequence = [1, 2, 3, 4, 5]

def square(x):
    return x**2
   
result = list(map(square, sequence))
print(result)

Filter

fiter takes in a sequence and returns a sequence containing only those elements of the sequence for which a function when applied to the element returns true. filter(function, sequence)

For example:

sequence = range(-10, 10)
greater_than_zero = list(filter(lambda x: x > 0, sequence))
print(greater_than_zero)

In [ ]:
sequence = range(-10, 10)
greater_than_zero = list(filter(lambda x: x > 0, sequence))
print(greater_than_zero)

Reduce

reduce performs computations on a sequence and returns a single result. reduce(function, sequence)

For example:

from functools import reduce
product = reduce((lambda x, y: x * y), [1, 2, 3, 4])
print(product)

At each step the function passed to reduce is passed two arguments, the current reduced value and the next value from the sequence. By default, the first item in the sequence is used as the initial value of the reduction value.


In [ ]:
from functools import reduce
product = reduce((lambda x, y: x * y), [1, 2, 3, 4])
print(product)

Other concepts to include:

pure functions, function composition, currying and partial application, recursive data structures, algebraic data types, monadic composition, type classes, lenses, monad transformers etc. https://hackernoon.com/why-functional-programming-matters-c647f56a7691 http://lambdaconf.us/downloads/documents/lambdaconf_slfp.pdf

http://www.cse.chalmers.se/~rjmh/Papers/whyfp.pdf


In [ ]:

Functional Programming Concepts

Pure Functions

This is easily the foremost rule of functional programming. Pure functions meet two fundamental restrictions:

  1. A pure function called multiple times with the same arguments will always return the same value. Always.
  2. No side effects occur throughout the function’s execution.

Immutable Values

In Fuctional Programming variables are immutable, no variable can (or should if the language does not enforce immutability) be modified after it's been initialised.

That means there is no assignment, only intialisation.

Monads

First Class Function

For a function to be first-class, you just have to be able to set it to a variable. That’s it.

def square(x):
    return x ** 2

f = square
print(f(2))

High Order Functions

High Order Functions build on the concept of First Class Functions, they are functions that either accept another function as an argument, or that return a function themselves. Common examples of higher-order functions are the functions map and filter which typically iterate over a sequence/container.

#


In [ ]:


In [ ]: