What is functional programming and it’s most important aspects?
Functional programming is an interesting programming concept which gains a lot of attention lately. This article presents some of the most important aspects of functional programming in general and provides several examples in Python.
Functional programming is a kind of the declarative programming paradigm where functions represent relations among objects, like in mathematics. Thus, functions are much more than ordinary routines.
In this article, you’ll find explanations on several important principles and concepts related to functional programming:
- pure functions,
- anonymous functions,
- recursive functions,
- first class functions,
- immutable data types.
A pure function is a function that:
- is idempotent — returns the same result if provided the same arguments,
- has no side effects.
If a function uses an object from a higher scope or random numbers, communicates with files and so on, it might be impure because its result doesn’t depend only on its arguments.
A function which modifies objects outside of its scope, write to files, prints to the console and so on, has side effects and might be impure as well.
Pure functions usually don’t use objects from outer scopes and thus avoid shared states. This might simplify a program and help escape some errors.
Anonymous (lambda) functions can be very convenient for functional programming constructs. They don’t have names and usually are created ad-hoc, with a single purpose.
In Python, you create an anonymous function with the lambda keyword:
The above statement creates the function that accepts two arguments and returns their sum. In the next example, the functions f and g do the same thing:
A recursive function is a function that calls itself during the execution. For example, we can use recursion to find the factorial in the functional style:
Alternatively, we can solve the same problem with the while or for loop:
First Class Functions
In functional programming, functions are the first-class objects, also called higher-order functions — the data types treated the same way as other types.
Functions (or, to be more precise, their pointers or references) can be passed as arguments to and returned from other functions. They can also be used as variables inside programs.
The code below illustrates passing the built-in function max as an argument of the function f and calling it from inside f.
Very important functional programming concepts are:
They are all supported in Python.
Mapping is performed with the built-in class map. It takes a function (or method, or any callable) as the first argument and an iterable (like a list or tuple) as the second argument, and returns the iterator with the results of calling the function on the items of the iterable:
In this example, the built-in function abs is called with the arguments -2, -1, 0, 1 and 2, respectively. We can obtain the same result with the list comprehension:
We don’t have to use built-in functions. It is possible to provide a custom function (or method, or any callable). Lambda functions can be particularly convenient in such cases:
The statement above multiplied each item of the list [-2, -1, 0, 1, 2] with 2 using the custom (lambda) function lambda item: 2 * item. Of course, we can use the comprehension to achieve the same thing:
Filtering is performed with the built-in class filter. It also takes a function (or method, or any callable) as the first argument and iterable as the second. It calls the function on the items of the iterable and returns a new iterable with the items for which the function returned True or anything that is evaluated as True. For example:
The statement above returns the list of non-negative items of [-2, -1, 0, 1, 2], as defined with the function lambda item: item >= 0. Again, the same result can be achieved using the comprehension:
Reducing is performed with the function reduce from the module functools. Again, it takes two arguments: a function and iterable. It calls the function on the first two items of the iterable, than on the result of this operation and the third item and so on. It returns a single value. For example, we can find the sum of all items of a list like this:
This example is just for illustration. The preferred way of calculating the sum is using the built-in reducing function sum:
Immutable Data Types
An immutable object is the object whose state can’t be modified once it’s created. Contrary, a mutable object allows changes of its state.
Immutable objects are generally desirable in functional programming.
For example, in Python, lists are mutable, while tuples are immutable:
We can modify lists by appending them new elements, but when we try to do this with tuples, they are not changed but new instances are created:
Advantages of Functional Programming
The underlying concepts and principles — especially higher-order functions, immutable data and the lack of side effects — imply important advantages of functional programs:
- they might be easier to comprehend, implement, test and debug,
- they might be shorter and more concise (compare two programs for calculating the factorial above),
- they might be less error-prone,
- they are easier to work with when implementing parallel execution.
Functional programming is a valuable paradigm worth learning. In addition to the advantages listed above, it’ll probably give you a new perspective of solving programming problems.