Using curried higher-order functions

While currying is simple to visualize using ordinary functions, the real value shows up when we apply currying to higher-order functions. In an ideal situation, the functools.reduce() function would be curryable so that we would be able to do this:

sum = reduce(operator.add)
prod = reduce(operator.mul)

This doesn't work, however. The built-in reduce() function isn't curryable by using the PyMonad library, so the above examples don't actually work. If we define our own reduce() function, however, we can then curry it as shown previously.

Here's an example of a home-brewed reduce() function that can be used as shown earlier:

from collections.abc import Sequence
from pymonad import curry
@curry def myreduce(function, ...

Get Functional Python Programming - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.