The PyMonad Library
This is similar in some respects to the functools.partial() function. The important
difference is that currying creates a function that can work in a variety of ways. The
functools.partial() function creates a more specialized function that can only be
used with the given set of bound values.
Here's an example of creating some additional curried functions:
g_t= systolic_bp(25, 50)
g_t(1, 0)
116.09
g_t(0, 1)
121.59
This is a gender-based treatment function based on our initial model. We must
provide gender and treatment values to get a final value from the model.
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 the ideal situation, the
functools.reduce() function would be "curryable" so that we can do this:
sum= reduce(operator.add)
prod= reduce(operator.mul)
The reduce() function, however, isn't curryable by the pymonad library, so this
doesn'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:
import collections.abc
from pymonad import curry
@curry
def myreduce(function, iterable_or_sequence):
if isinstance(iterable_or_sequence, collections.abc.Sequence):
iterator= iter(iterable_or_sequence)
else:
iterator= iterable_or_sequence
s = next(iterator)
for v in iterator:
s = function(s,v)
return s