Skip to Content
Functional Python Programming - Second Edition
book

Functional Python Programming - Second Edition

by Steven F. Lott
April 2018
Intermediate to advanced content levelIntermediate to advanced
408 pages
10h 42m
English
Packt Publishing
Content preview from Functional Python Programming - Second Edition

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, ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Functional Python Programming - Third Edition

Functional Python Programming - Third Edition

Steven F. Lott

Publisher Resources

ISBN: 9781788627061Supplemental Content