Higher order function to apply many functions to one argument

被刻印的时光 ゝ 提交于 2019-12-13 03:06:39

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!