Sunday 3 July 2011

Functional programming in python

     In python we can pass functions as an argument to another function. This is called higher order functions. Lets define two functions at the interactive python prompt and try some experiments.
>>> def sqr(x): return x*x
...
>>> def cube(x): return x*x*x
... 
>>> def compose(f,g,x): return f(g(x))
...
>>> compose(sqr,cube,3)
729
    In this example we define two functions sqr() and cube(), then we pass these functions as an argument to the another function compose(). Compose function perform the computstion and return the result.
    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
The lambda keyword is used for creating anonymous functions. The body of the lambda should be composed of simple expressions only. More than one arguments are supported, these are separated by commas. Lambda is often used in conjunction with map(),filter() and reduce().
Syntax:
lambda argument_list:expression
Example:
>>> f=lambda x:x+4
>>> f(3)
7
In the above example, we use lambda to create a function which accepts an argument and returns it after adding four.
>>> f=lambda x,y:x+y
>>> f(5,4)
9
    In this example lambda take two arguments and return result after adding the values.
filter()
Filter function accepts a function and a list as arguments and returns the list composed of only those elements for which the function returns true.
syntax:
filter(function,sequence)
Example:
>>> a=[2,3,6,7,11]
>>> output=filter(lambda x:x%2,a)
>>> print output
[3, 7, 11]
Here function 'lambda' will be applied to every element of the list a. If the function return true for a particular list of elements, that will be included in the result list.
map()
Map function accepts a function and a list as arguments and returns the list obtained by applying the function on each element of the original list.
Syntax:
map(function,sequence)
Example
>>> def sqrt(x): return x*x
...
>>> map(sqrt,[2,6,3,1,9])
[4, 36, 9, 1, 81]
     map function accept a function sqrt and a list of elements, then it apply the function to each element of the list and retun the result list.
This example can also be written by using lambda.
>>> map(lambda x:x*x,[2,6,3,1,9])
[4, 36, 9, 1, 81]
reduce()
     Important thing is that, reduce() returns only a single value. It also accept a function and a list of elements, at 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.
Syntax:
reduce(function,sequence)
Consider the following example which uses reduce to compute the sum of a list of numbers.
Example
>>> nums=[1,2,3,10,20,30]
>>> def add(x,y): return x+y
...
>>> s=reduce(add,nums)
>>> print s
66
map – reduce
Note that the return value of a map is a list while the input of reduce is a list. Thus, the two functions can be combined to a great effect.
Example
>>> a=[1,2,3,10,200,3000]
>>> b=[20,30,40,10,3,2]
>>> def add(x,y):
... return x+y
...
>>> def mul(x,y):
... return x*y
...
>>> result=reduce(add,map(mul,a,b))
>>> print result
6900
     We don't actually have to define the add and mul function above. We can simply use operator.add and operator.mul from the operator module.
Example
>>> import operator
>>> A=[1,2,3,10,200,3000]
>>> B=[20,30,40,10,3,2]
>>> s=reduce(operator.add,map(operator.mul,A,B))
>>> print s
6900

No comments:

Post a Comment