sealed trait Either[+E, +A] {
def map[B](f:A=>B): Either[E,B] = this match {
case Right(a) => Right(f(a))
case Left(e) => Left(e)
}
def flatMap[EE >: E, B](f: A => Either[EE, B]): Either[EE, B] =
this match {
case Right(a) => f(a)
case Left(e) => Left(e)
}
def orElse[EE >: E, B >: A](b: => Either[EE,B]): Either[EE, B] =
this match {
case Right(a) => Right(a)
case Left(e) => b
}
def map2[EE >: E, B, C](b: Either[EE,B])(f:(A,B)=>C): Either[EE,C] =
a flatMap (aa =>
b map (bb =>
f(aa, bb)))
}
case class Left[+E](value: E) extends Either[E, Nothing]
case class Right[+A](value: A) extends Either[Nothing, A]
Can someone explain to me how this long sequence of code can be broken down and understood ?
Thanks in advance.