Understanding Functional Programming in Python: Map, Filter, Reduce, Lambda, and Partial Application
This article introduces the functional programming paradigm, contrasts it with imperative programming, and demonstrates core Python functional tools such as map, filter, reduce, lambda expressions, and partial application, including code examples and explanations of side‑effects and higher‑order functions.
This article introduces the functional programming paradigm and shows how to apply functional programming concepts in Python.
It contrasts the imperative paradigm, where a sequence of commands changes program state, with the functional paradigm, where you describe what should be computed without mutating variables.
The text explains side effects, emphasizing that pure functions should not modify external state and must return new values.
Example code demonstrates a global variable being changed inside a function, illustrating why such mutation is discouraged in functional programming:
<code>a = 3
def some_func():
global a
a = 5
some_func()
print(a)</code>The article then introduces map , explaining that it applies a function to each element of an iterable. It shows how to create a custom iterable with a Counter class and iterate over it:
<code>class Counter:
def __init__(self, low, high):
self.current = low
self.high = high
def __iter__(self):
return self
def __next__(self):
if self.current > self.high:
raise StopIteration
else:
self.current += 1
return self.current - 1
for c in Counter(3, 8):
print(c)
# Output: 345678</code>It shows a simple use of map to square numbers in a list:
<code>x = [1, 2, 3, 4, 5]
def square(num):
return num * num
print(list(map(square, x)))</code>Because Python functions are lazy, the article notes that without wrapping the result in list() , map returns an iterator.
Next, lambda expressions are presented as anonymous single‑line functions. Example:
<code>square = lambda x: x * x
print(square(3)) # 9</code>The same squaring operation can be written concisely with map and a lambda:
<code>x = [1, 2, 3, 4, 5]
print(list(map(lambda num: num * num, x)))</code>The article then covers reduce , which collapses an iterable into a single value. Using functools.reduce to compute the product of a list:
<code>from functools import reduce
product = reduce(lambda x, y: x * y, [1, 2, 3, 4])
print(product) # 24</code>It explains filter , which selects elements that satisfy a predicate. Example filtering negative numbers:
<code>x = range(-5, 5)
all_less_than_zero = list(filter(lambda num: num < 0, x))
print(all_less_than_zero) # [-5, -4, -3, -2, -1]
</code>Higher‑order functions are discussed, showing how functions can be passed as arguments and returned. Example:
<code>def summation(nums):
return sum(nums)
def action(func, numbers):
return func(numbers)
print(action(summation, [1, 2, 3])) # 6
</code>Finally, the concept of partial application (using functools.partial ) is introduced to fix some arguments of a function, creating specialized versions such as a square function from a generic power function:
<code>from functools import partial
def power(base, exponent):
return base ** exponent
square = partial(power, exponent=2)
print(square(2)) # 4
</code>Overall, the article provides a concise tutorial on functional programming tools in Python, highlighting their benefits for writing clear, side‑effect‑free code.
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.