How are Functors useful?

前端 未结 3 777
轮回少年
轮回少年 2021-02-04 18:17

We know that any generic type F[_] withmap method, which complies to some laws, is a functor. For instance, List[_], Option

3条回答
  •  青春惊慌失措
    2021-02-04 18:54

    I don't know Scala, but in Haskell, the Functor class is essential to defining Van Laarhoven-style lenses:

    type Lens' s a = forall f . Functor f => (a -> f a) -> s -> f s
    

    These lenses are typically defined for specifically-related types s and a, but it's essential to their utility that they work with an arbitrary functor.

    Functor is also important in its role as a superclass of Applicative and of Traversable. When working with these more powerful abstractions, it's often very useful to reach for the fmap method.

提交回复
热议问题