Functional programming in Python

Python supports functional programming paradigm which treats computation as mathematical evaluation of functions. Functions are first-class objects in Python. That is they have attributes and can be referenced and assigned to variables. Python also supports higher-order functions, that can accept other functions as arguments and return functions to the caller.

The functional programming paradigm encourages us to structure our programs as collections of pure functions. The basic element of Functional programming in python are the functions map(),reduce(),and filter() , and the operator lambda.

Lambda

Python supports the creation of anonymous functions (i.e. functions that are not bound to a name) at runtime, using a construct called “lambda”.The lambda definition does not include a “return” statement — it always contains an expression which is returned. Also we can put a lambda definition anywhere a function is expected, and we don’t have to assign it to a variable at all.

Syntax:  lambda argument_list:expression

The following shows an example of lambda function:

>>> def increment(n):
. . .       return lambda x: x + n
. . .
>>> a = increment(2)
>>> b = increment(5)
>>>
>>> print a(10), b(4)
12  9
>>>
>>> print increment(11)(12)
23
>>>

filter()

Syntax: filter(function, sequence)
filter() return those items of sequence for which function(item) is true. If function is None, return the items that are true. If sequence is a tuple
or string, return the same type, else return a list.

The following shows an example of filter():

>>> def even(num):
. . .       return num % 2 == 0
. . .
>>> n = [1,2,4,7,3,9,8,6]
>>> filter(even, n)
[2, 4, 8, 6]
>>>

map()
map() return a list of the results of applying the function to the items of the argument sequences. If more than one sequence is given, the function is called with an argument list consisting of the corresponding item of each sequence, substituting None for missing values when not all sequences have the same length. If the function is None, return a list of the items of the sequence (or a list of tuples if more than one sequence).

>>> def test(x):
. . .       return (x*x)+x+10
. . .
>>> map(test, [1,2,3,4,5])
[12, 16, 22, 30, 40]
>>>

reduce()

reduce() function reduces a list to a single value by combining elements via a supplied function. It returns only a single value. The following function describes working of reduce() function.First it take the first two elements in the list and applied it to the function, in the next step the result of the previous step and the next element is applied to the function, this process will continue at the end of the list, finally a single value returned.

>>> l = [1,2,3,4,5]
>>> def mul(a,b):
. . .       return a*b
. . .
>>> reduce(mul, l)
120
>>>

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

The WordPress.com Blog

The latest news on WordPress.com and the WordPress community.

%d bloggers like this: