Haskell编码风格:map,fmap或< $> ;? [英] Haskell coding-style: map, fmap or <$>?
问题描述
有任何理由比其他人喜欢下列符号之一,或者这只是一个偏好问题?
Is there any reason to prefer one of the following notations over the others or is this simply a matter of preference?
map toLower "FOO"
fmap toLower "FOO"
toLower <$> "FOO"
另外:我意识到< $&
与`fmap`
相同。我是否正确的假设 map
只是一个较不通用的形式 fmap
?
As an aside: I realize that <$>
is the same as `fmap`
. Am I right in the assumption that map
is just a less general form of fmap
?
推荐答案
正如你所说, map
code> fmap 。如果你知道你有一个列表,然后我会使用 map
,因为它使代码更清晰,如果你犯了错误的错误消息可能会更少混乱。但在很大程度上是一个偏好问题。
As you say, map
is a less general form of fmap
. If you know you have a list then I would use map
as it makes the code clearer and if you make a mistake the error message is likely to be less confusing. However to a large extent it's a matter of preference.
(< $>)
fmap
。直到GHC 7.10它没有被Prelude导出,所以在默认情况下不可用 - 但是即使使用旧的GHC版本,也很容易从 Data.Functor
或 Control.Applicative
,这些天几乎是标准的做法。
(<$>)
is the same as fmap
. Until GHC 7.10 it wasn't exported by the Prelude so wasn't available by default - but even with older GHC versions it's easy to import from Data.Functor
or Control.Applicative
and these days it's pretty much the standard way to do this.
这篇关于Haskell编码风格:map,fmap或< $> ;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!