问题
I want the common name of a higher order function that applies a list of functions onto a single argument.
In this sense it is a converse of map. map
takes a function and a list of arguments and applies that function to the entire list. In Python it might look like this
map = lambda fn, args: [fn(arg) for arg in args]
I'm thinking of the function that does the same but has the alternate argument as a list type
??? = lambda fns, arg: [fn(arg) for fn in fns]
I suspect that this function exists and has a common name. What is it?
回答1:
Actually, what you describe is not the converse of map
, but just map
applied in another way. Consider, e.g.,
map(lambda f: f(2), [lambda x: x + 1, lambda x: x, lambda x: x * x])
which in effect applies three functions (plus 1, identity, and squaring) to the argument 2
. So what you wanted is just
alt_map = lambda x, fns: map(lambda f: f(x), fns)
which is still an ordinary map.
来源:https://stackoverflow.com/questions/14758077/higher-order-function-to-apply-many-functions-to-one-argument