[Python-Dev] functools.compose to chain functions together

Xavier Morel xavier.morel at masklinn.net
Mon Aug 17 12:38:57 CEST 2009


On 17 Aug 2009, at 09:43 , Steven D'Aprano wrote:
> On Mon, 17 Aug 2009 08:10:16 am Martin v. Löwis wrote:
>
>> I don't think he did. Comparing it to the one obvious solution (use
>> a lambda expression), his only reasoning was "it is much easier to
>> read". I truly cannot believe that a compose function would be
>> easier to read to the average Python programmer: if you have
>>
>>  def foo(data):
>>    return compose(a, b(data), c)
>>
>> what would you expect that to mean?
>
> foo is a factory function that, given an argument `data`, generates a
> function b(data), then composes it with two other functions a and c,
> and returns the result, also a function.
>
 From his messages, I think Martin's issue with `compose` is with the  
composition order rather than the fact that it "pipes" functions:  
compose uses the mathematical order, (f ∘ g)(x) = f(g(x)) (so g, the  
last function of the composition, is applied first), rather than a  
"shell pipe" order of `(f >>> g)(x) = g(f(x))` (where g, the last  
function of the composition, is applied last).

> For the record, Haskell makes compose a built-in operator:
>
> http://www.haskell.org/haskellwiki/Function_composition

Yes, but Haskell also has a left-to-right composition, the (>>>)  
operator: http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Arrow.html#v 
:>>>


More information about the Python-Dev mailing list