0

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)    
max
  • 49,282
  • 56
  • 208
  • 355
  • what you've done is specialize compose with regard to non_negative. – Dan D. Jan 25 '12 at 16:50
  • @DanD. Thx.. I am not familiar with the functional programming, so I didn't realize there's a term for that. Could you refer me to some introductory materials that discuss that? – max Jan 25 '12 at 17:03
  • function specialization is [partial evaluation](http://www.google.com/search?q=partial+evaluation) of a function with regard to one or more of its arguments. it can be done with a program which by default is what is implied by the term but it can also be done, as you have, by hand. the code in the second sample, `non_negative` is a specialized version of `compose` with regard to the `non_negative` function from the first code sample. – Dan D. Jan 26 '12 at 01:47
  • 1
    Strictly speaking your modification example doesn't work, as you don't return `new_func`, so `g` just ends up being `None`. – Ben Jan 26 '12 at 02:17
  • @Ben: oops, fixed. @DanD I see. It seems this specialization would restrict the reusability of `non-negative`, since it becomes limited to one signature. – max Jan 26 '12 at 05:12

2 Answers2

2

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.

Ben
  • 68,572
  • 20
  • 126
  • 174
1

Your two examples amount to exactly the same thing, except that in one case you use a point-free style, and in the other you don't.

The only consideration is which you (and whoever reads your code) find the most readable. In certain cases, I find the point free style most natural; this is one of those cases.

Marcin
  • 48,559
  • 18
  • 128
  • 201
  • I didn't know what "point-free" means. But of course, SO has the answer: http://stackoverflow.com/questions/944446/what-is-point-free-style-in-functional-programming. – max Jan 25 '12 at 20:57
  • Can I ask you why you find point free style more natural in this case? It does require more typing... – max Jan 25 '12 at 21:07
  • @max: Assuming compose is a library function, it takes rather less. – Marcin Jan 25 '12 at 21:27
  • `compose` is a library function; but I'm comparing `non_negative(f)` vs `compose(non_negative, f)`. – max Jan 25 '12 at 21:32
  • @max: Yes, but decorator style is harder to understand. – Marcin Jan 25 '12 at 21:36