高阶函数可将多个函数应用于一个参数 [英] Higher order function to apply many functions to one argument

查看:120
本文介绍了高阶函数可将多个函数应用于一个参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个高阶函数的通用名称,该函数将函数列表应用于单个参数.

I want the common name of a higher order function that applies a list of functions onto a single argument.

从这个意义上讲,它是map的反面. map接受一个函数和一个参数列表,并将该函数应用于整个列表.在Python中,可能看起来像这样

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?

推荐答案

实际上,您所描述的不是map的反义词,而只是以另一种方式应用的map.考虑例如

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])

实际上将三个函数(加1,标识和平方)应用于参数2.所以你想要的只是

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)

仍然是普通地图.

这篇关于高阶函数可将多个函数应用于一个参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆