如何将可遍历的函数应用于一个值 [英] How to apply a traversable of functions to a one value

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

问题描述

如果我想要类似的东西我应该用什么

What should I use if I want to have something like

[a->b] -> a -> [b]

基本上我有一个函数列表,所有函数都接受一个值 a 并返回 b.我想将它们全部应用到一个 a 并得到结果 [b].

basically I have a list of functions, all take in a value a and returns b. I want to apply all of them to one a and get the results [b].

我应该使用哪个?

谢谢

推荐答案

你不需要Traversable,只需要Functor:

swingMap f x = fmap ($ x) f

另见 swing 函数(这相当于swing fmap).

或者,如果您使用的是 Edward Kmettdistributive,您可以充分利用这两个答案(只有一个 Functor 而不是 Traversable) 和 chi 的答案(避免为工作),通过使用 分发来自Data.Distributive.

Or, if you're using Edward Kmett's distributive library, you can have the best of both this answer (only a Functor rather than a Traversable) and chi's answer (avoiding defining a custom function for the job), by using distribute from Data.Distributive.

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

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