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

F

fp2161

I don't think "obvious" is quite the right description. Well, perhaps

"obviously wrong" :)
You also need to define func1 (trivial), func2, func3, func5, func6,

func7, func8, ..., func2147483647, plus another master function to choose

between them, depending on the number of functions provided as argument.
I assume that the maximum number of arguments given is 2**31-1. Python

may not actually have that limitation, in which case you would need to

define additional functions.
Or... you would have to come up with an implementation which doesn't hard-

code the number of functions used.

Steven

I got the generalisation criticism before yours, and generalised it accordingly. Unfortunately it was wrong essentially because it was so obvious that Josh Englsih posted essentially the same one before me...
 
F

fp2161

They're not bad practice; all they are is a function without a name,

that's restricted to returning a single expression. So they're

perfectly suited to this task, and ill-suited to some others.



Like everything, lambda's a tool that can be used or misused.



ChrisA

For this purpose however, I suspect that a named function with a proper docstring that can be imported and reused over and over again is probably more appropriate than a lambda (the first post is telling us about something happening from time to time...)
 
F

fp2161

Reduce tricks are nice, but I prefer clarity sometimes:



def double(x):

return x*2



def add3(x):

return x+3





def compose(*funcs):

for func in funcs:

if not callable(func):

raise ValueError('Must pass callable functions')



def inner(value):

for func in funcs:

value = func(value)

return value



return inner





add_then_double = compose(add3, double)

double_then_add = compose(double, add3)



print add_then_double(1) # prints 8

print double_then_add(1) # prints 5


This is my favourite design, simple, clear, straightforward, very pythonic imho. So great that I actually dod not notice it, and wrote it again afteryou! Imho still, the ValueError you are raising is not that important in this context, it would raise an Error anyway.
 
A

alex23

For this purpose however, I suspect that a named function with a proper docstring that can be imported and reused over and over again is probably more appropriate than a lambda

Given that in Chris' example the lambda was returned from a factory,
importing the inner function by name is never going to be a concern.

It's also possible to assign to both __name__ and __doc__ for a lambda,
which can be useful at times.
 
A

alex23

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.
 
F

Fabrice Pombet

Given that in Chris' example the lambda was returned from a factory,

importing the inner function by name is never going to be a concern.



It's also possible to assign to both __name__ and __doc__ for a lambda,

which can be useful at times.
I am sorry, I can't see any of Chris' code in this thread, where is it???
 
F

Fabrice Pombet

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,810
Latest member
Kassie0918

Latest Threads

Top