Back to Fpinscala

06.Answer

answerkey/errorhandling/06.answer.md

latest518 B
Original Source
scala
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 Left(e) => Left(e)
    case Right(a) => f(a)

def orElse[EE >: E, AA >: A](b: => Either[EE, AA]): Either[EE, AA] =
  this match
    case Left(_) => b
    case Right(a) => Right(a)

def map2[EE >: E, B, C](b: Either[EE, B])(f: (A, B) => C): Either[EE, C] =
  for
    a <- this
    b1 <- b
  yield f(a,b1)