I have numerous reusable functions, all with the same signature (they take a record
and return a float
). I often need to combine functions into a new function.
Let's say I want to create a function that takes a record
, applies f
to it, and if the result is negative converts it to zero. I have two ways of doing that: composition and function modification. What are the pros and cons of each approach?
Composition:
def non_negative(value):
return max(0, value)
g = compose(non_negative, f)
# from functional module by Collin Winter
def compose(func_1, func_2, unpack=False):
"""
compose(func_1, func_2, unpack=False) -> function
The function returned by compose is a composition of func_1 and func_2.
That is, compose(func_1, func_2)(5) == func_1(func_2(5))
"""
if not callable(func_1):
raise TypeError("First argument to compose must be callable")
if not callable(func_2):
raise TypeError("Second argument to compose must be callable")
if unpack:
def composition(*args, **kwargs):
return func_1(*func_2(*args, **kwargs))
else:
def composition(*args, **kwargs):
return func_1(func_2(*args, **kwargs))
return composition
Modification:
def non_negative(func):
def new_func(record):
return max(0, func(record))
return new_func
g = non_negative(f)
Assuming compose
is available in a library, then I would prefer that style for this example.
The main reason is that it separates out the concerns of clamping a value to non-negative values and feeding the result of one function to another. In the "modification" style, if you ever find yourself wanting to run non_negative
on a value rather than on the result of a function, you'll end up with contortions like non_negative(lambda x: x)(value)
. And you need to write a separate function for every thing function you might want to compose, every single one of which contains the composition logic mixed in with the code for that function.
In this example, the burden is trivial whichever way you do it. But generally, if it's this easy to make small independent pieces that you then glue together to make your composite code, that's the good way to do it.