When to Use Currying?
Let’s look at the scenarios where you should make use of currying.
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):
Press + to interact
from functools import partialdef quad(a, b, c, x):return a*x*x + b*x + cc = [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 quad
...