Scala Functor and Monad differences

后端 未结 5 1326
再見小時候
再見小時候 2020-12-22 19:01

Can please someone explain the differences between Functor and Monad in the Scala context?

5条回答
  •  时光取名叫无心
    2020-12-22 19:15

    The best article laying out in details those two notions is "The Essence of the Iterator Pattern " from Eric Torreborre's Blog.

    Functor

    trait Functor[F[_]] {
      def fmap[A, B](f: A => B): F[A] => F[B]
    }
    
    • One way of interpreting a Functor is to describe it as a computation of values of type A.
      For example:
      • List[A] is a computation returning several values of type A (non-deterministic computation),
      • Option[A] is for computations that you may or may not have,
      • Future[A] is a computation of a value of type A that you will get later, and so on.
    • Another way of picturing it is as some kind of "container" for values of type A.

    It is the basic layer from which you define:

    • PointedFunctor (to create a value of type F[A]) and
    • Applic (to provide a method applic, being a computed value inside the container F (F[A => B]), to apply to a value F[A]), Applicative Functor (aggregation of an Applic and a PointedFunctor).

    All three elements are used to define a Monad.

提交回复
热议问题