Libraries Supporting Composition
Learn to compose curried functions using the * operator in PyMonad.
We'll cover the following
Existing libraries supporting composition
There are quite a few open-source libraries that provide functional programming support, including a compose function.
funcy
andfn.py
are two such libraries that can be found on github.com or installed with pip.
PyMonad
We will concentrate on PyMonad
in this section. It provides composition of curried functions (see the PyMonad
@curry
decorator described earlier in this chapter).
Rather than using a compose function, PyMonad uses a compose operator, *
.
Here is how it works.
Example one
Assume that our task is to create an iterator that counts down. We start by creating curried versions of the built-in reversed
and range
functions as shown below, from line 3
to line 9
. Once these functions are defined, composing them is quite neat (line 11
).
Get hands-on with 1400+ tech skills courses.