Anu*_*ain 8 haskell functor typeclass traversable
我正在查看文档Data.Traversable并发现fmapDefault- https://downloads.haskell.org/~ghc/latest/docs/html/libraries/base/Data-Traversable.html#g:3
fmapDefault :: Traversable t => (a -> b) -> t a -> t b
文件说明 -
如果定义了遍历,则此函数可用作Functor实例中fmap的值.
所以可能它可以用于派生fmap一个Traversable实例.然而,Traversable有Functor一个超类.
class (Functor t, Foldable t) => Traversable t where
    ...
因此,如果不先定义Traversable实例,就无法定义Functor实例!无论你有什么Traversable,你都可以访问fmap,这相当于(也许更有效)fmapDefault.
那么人们会在哪里使用fmapDefault,而不是更熟悉fmap?
lef*_*out 11
它允许你写
data Foo a = ...
instance Functor Foo where -- we do define the functor instance, but we “cheat”
  fmap = fmapDefault       -- by using `Traversable` in its implementation!
instance Traversable Foo where
  traverse = ...           -- only do this manually.
那就是说,我不认为这是真的明智.Functor实例通常很容易手工完成,明显的实现确实可能比Traversable派生实例更有效.通常,实例实际上可以自动创建:
{-# LANGUAGE DeriveFunctor #-}
data Foo a = ...
       deriving (Functor)
| 归档时间: | 
 | 
| 查看次数: | 133 次 | 
| 最近记录: |