Search⌘ K
AI Features

When to Use Currying?

Explore the differences between currying and partial application in Python. Understand how currying with decorators works and its impact on code readability and efficiency. Learn to decide when currying is appropriate, especially in functional programming contexts, and when partial application might be clearer for code maintenance.

We'll cover the following...

Currying vs. partial application

Currying and partial application both do similar jobs. We will use them both in an example using map.

Here is the example using partial application (using the functools partial function):

Python 3.8
from functools import partial
def quad(a, b, c, x):
return a*x*x + b*x + c
c = [1, 2, 3, 4, 5]
x = [2, 4, 6, 8, 10]
m = map(partial(quad, 1, 2), c, x)
print(list(m))

We have created a partial function of quad, setting a to 1 and b to 2. We then map this function onto two lists containing the c and x values.

How would we do this with currying? We would use our curried version of ...