Can please someone explain the differences between Functor and Monad in the Scala context?
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 typeA
.
For example:
List[A]
is a computation returning several values of typeA
(non-deterministic computation),Option[A]
is for computations that you may or may not have,Future[A]
is a computation of a value of typeA
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
.