How are Functors useful?

前端 未结 3 774
轮回少年
轮回少年 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:46

    Well, once you know something is a Functor, you don't just get map, you get all of the functions you can derive with it too

    For example, it's possible to derive the function lift in a way that works for any functor.

    Lift will "lift" a function from A => B to F[A] => F[B] for some Functor F[_] and is defined as

    def lift[A, B](f: A => B): F[A] => F[B] = map(_)(f)
    

    If you are using a library like cats or scalaz then you get these functions for free. The cats documentation has a few other examples you might be interested in

提交回复
热议问题