Is there a function that applies list of functions to a value?

Fabrice Pombet fp2161 at gmail.com
Fri Aug 30 04:11:50 EDT 2013


On Friday, August 30, 2013 8:36:40 AM UTC+2, alex23 wrote:
> On 30/08/2013 4:17 PM, fp2161 at gmail.com wrote:
> 
> > On Wednesday, August 28, 2013 8:50:53 PM UTC+2, Josh English wrote:
> 
> >> def compose(*funcs):
> 
> >>      for func in funcs:
> 
> >>          if not callable(func):
> 
> >>              raise ValueError('Must pass callable functions')
> 
> 
> 
> > Imho still, the ValueError you are raising is not that important in this context, it would raise an Error anyway.
> 
> 
> 
> The main advantage with Josh's approach is that it fails at the point of 
> 
> composition, not when the composed function is first used.  It'd be even 
> 
> more useful if it aggregated a list of the failing functions and 
> 
> returned their names as part of the error.
> 
> 
> 
> Personally, I'd go with an assertion:
> 
> 
> 
>      assert all(map(callable, funcs)), "Must pass callable functions"
> 
> 
> 
> I find that it makes it more obvious that this is part of the function 
> 
> contract rather than the actual body.

it is a valid point, but I would contend that it makes this quick and easy code a little bit heavy just for the sake of ensuring that you are composing composable functions... The assertion is definitely better.



More information about the Python-list mailing list