Functional programming is a programming paradigm based on breaking down a problem into a set of individual functions. Ideally, every function only takes a set of input arguments and produces an output.
In functional programming, functions don’t have any internal state that affects the output that they produce for a given input. This means that anytime you call a function with the same set of input arguments, you’ll get the same result or output.
In a functional program, input data flows through a set of functions. Each function operates on its input and produces some output. Functional programming tries to avoid mutable data types and state changes as much as possible. It works with the data that flow between functions.
Other core features of functional programming include the following:
There are several important concepts in this list. Here’s a closer look to some of them:
Since Python is a multi-paradigm programming language, it provides some tools that support a functional programming style:
[lambda](https://realpython.com/python-lambda/)
[functools](https://docs.python.org/3/library/functools.html#module-functools)
and [itertools](https://realpython.com/python-itertools/)
[map()](https://docs.python.org/3/library/functions.html#map)
, [filter()](https://docs.python.org/3/library/functions.html#filter)
, [reduce()](https://docs.python.org/3/library/functools.html#functools.reduce)
, [sum()](https://docs.python.org/3/library/functions.html#sum)
, [len()](https://docs.python.org/3/library/functions.html#len)
, [any()](https://realpython.com/any-python/)
, [all()](https://docs.python.org/3/library/functions.html#all)
, [min()](https://docs.python.org/3/library/functions.html#min)
, [max()](https://docs.python.org/3/library/functions.html#max)
, and so onEven though Python isn’t heavily influenced by functional programming languages, back in 1993 there was a clear demand for some of the functional programming features listed above.
In response, several functional tools were added to the language. According to Guido van Rossum, they were contributed by a community member:
Python acquired
lambda
,reduce()
,filter()
andmap()
, courtesy of (I believe) a Lisp hacker who missed them and submitted working patches. (Source)
Over the years, new features such as list comprehensions, generator expressions, and built-in functions like sum()
, min()
, max()
, all()
, and any()
were viewed as Pythonic replacements for map()
, filter()
, and reduce()
. Guido planned to remove map()
, filter()
, reduce()
, and even lambda
from the language in Python 3.
Luckily, this removal didn’t take effect, mainly because the Python community didn’t want to let go of such popular features. They’re still around and still widely used among developers with a strong functional programming background.
In this tutorial, you’ll cover how to use Python’s reduce()
to process iterables and reduce them to a single cumulative value without using a [for](https://realpython.com/python-for-loop/)
loop. You’ll also learn about some Python tools that you can use in place of reduce()
to make your code more Pythonic, readable, and efficient.
#python #functional #pythonic style