Functional Programming in Python
Functional Programming in Python
Learn the basics of Python functional programming, including Nested Functions, Currying, Closures, Anonymous Functions, Tail Recursion, Concurrency, and more.
Join the DZone community and get the full member experience.Join For Free
The idea of this post is to understand functional programming concepts using Python.
1. Programming ParadigmsThere are three programming paradigms: Imperative Programming, Functional Programming and Logic Programming. Most of the programming languages support only imperative style. Imperative style is having direct relationship with machine language. The features of imperative styles like assignment operator, conditional execution and loops are directly derived from machine languages. The procedural and object oriented programming languages such as C, C++, Java are all imperative programming languages. Logic programming is completely a different style, it will not contain the solution to the problem instead written in terms of facts and rules. Prolog, ASP & DataLog are some of logic programming languages.
2. Functional programmingWikipedia definition says " In computer science, functional programming is a programming paradigm—a style of building the structure and elements of computer programs—that treats computation as the evaluation of mathematical functions and avoids changing-state and mutable data". Following are the characteristics of the functional programmings :
3. Type of Functions
3.1 Higher Order Functions:A function can take other function as a argument and may return a function.
def FindFormula(shape): if (shape == "Square"): return SquareArea if (shape == "Circle"): return CircleArea def SquareArea(a): return (a * a) def CircleArea(r): return (22/7*r * r) def findShape(sides): if (sides == 4): return "Square" if (sides == 0 ): return "Circle" if __name__ == "__main__": size = 5 area = FindFormula(findShape(sides=4))(size) print("Area = ",area)
3.2 Anonymous Functions
area = lambda x : 22/7*x*x
Anonymous functions are created with "lambda" keyword. We can use this syntax, where we need a function.3.3 Nested Functions
Functions can be defined within the scope of another function. This type of functions defined inside a other function. The inner function is only in scope inside the outer function. It is useful when the inner function is being returned or when it is being passed into another function.
def oFunction(x,y): def SqArea(x,y): return (x*y) return SqArea(x,y)
sum = oFunction(10,20)
In the above example, the function SqArea is strictly inside the scope of oFunction().
def curried_pow(x): def h(y): return pow(x,y) return h
def sumA(a): def sumB(b): return(a+b) return(sumB)
x = sumA(10) y = x(20) print (y)
3.6 Tail RecursionA recursive function is called as Tail Recursive when the last statement in the function makes the recursive calls only. It is much more efficient than the recursive function because, since the recursive call is the last statement, there is nothing to be saved in the stack for the current function call. Hence the tail recursion is very light on the memory utilization. Regular Recursive Function for Factorial in Python:
def fact(n): if (n == 0): return 1 else: return n * fact(n-1)
The same can converted to Tail Recursion as below, by having additional argument:
def fact(n): tailFact(n,1) def tailFact(n,a): if (n == 0): return(a) else: return tailFact(n-1,n*a)
4 Benefits of Functional Programming
5 SummaryWe have briefly discussed the functional programming paradigm & its benefits. The functional programming style is most suitable for developing algorithms, analytic, data mining and machine learning algorithm. Most of the modern programming languages are supporting functional styles, also Java is trying to catch up with this style in the latest version (Java 8).
Opinions expressed by DZone contributors are their own.