[英]How can I use functors or applicatives to rewrite this Haskell function over lists of tuples
Is there a nicer way to write the following function fs'
with functors or applicatives? 有没有更好的方法可以用函子或应用程序编写以下函数
fs'
?
fncnB = (* 2)
fncnA = (* 3)
fs' fs = zip (map (fncnA . fst) fs) $ map (fncnB . snd) fs
I see from this question that I could rely on the functor instance of lists to map a single function acting over both elements of each tuple, or eg on the applicative instance of tuples to apply functions to just the second-half of a two-tuple, but I'm curious how functors and applicatives fit into the picture of operating over lists of multi-component data types. 我从这个问题中看到,我可以依靠列表的函子实例来映射作用在每个元组的两个元素上的单个函数,或者例如在元组的应用实例上将函数仅应用于两个元组的后半部分,但我很好奇函子和应用程序如何适合对多组分数据类型列表进行操作的图片。
what you need is the mapPair function, it's defined in the utility-ht package but it's just 您需要的是mapPair函数,它是在Utility-ht包中定义的,但它只是
mapPair :: (a -> c, b -> d) -> (a,b) -> (c,d)
mapPair (f,g) (a,b) = (f a, g b)
use 采用
Prelude> mapPair ((* 2), (* 3)) (2,3)
(4,9)
or 要么
Prelude> map (mapPair ((* 2), (* 3))) [(1,2),(2,3)]
[(2,6),(4,9)]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.