Advertisement
Guest User

Untitled

a guest
Apr 20th, 2014
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 1.08 KB | None | 0 0
  1. sealed trait Either[+E, +A] {
  2.   def map[B](f: A => B): Either[E, B]
  3.   def flatMap[EE >: E, B](f: A => Either[EE, B]): Either[EE, B]
  4.   def orElse[EE >: E,B >: A](b: => Either[EE, B]): Either[EE, B]
  5.   def map2[EE >: E, B, C](b: Either[EE, B])(f: (A, B) => C): Either[EE, C]
  6. }
  7.  
  8. case class Left[+E](value: E) extends Either[E, Nothing] {
  9.   def map[B](f: (Nothing) => B): Either[E, B] = this
  10.   def flatMap[EE >: E, B](f: Nothing => Either[EE, B]): Either[EE, B] = this
  11.   def orElse[EE >: E,B >: Nothing](b: => Either[EE, B]): Either[EE, B] = b
  12.   def map2[EE >: E, B, C](b: Either[EE, B])(f: (Nothing, B) => C): Either[EE, C] = this
  13. }
  14.  
  15. case class Right[+A](value: A) extends Either[Nothing, A] {
  16.   def map[B](f: A => B): Either[Nothing, B] = {
  17.     Right(f(value))
  18.   }
  19.   def flatMap[EE >: Nothing, B](f: A => Either[EE, B]): Either[EE, B] = {
  20.     f(value)
  21.   }
  22.   def orElse[EE >: Nothing,B >: A](b: => Either[EE, B]): Either[EE, B] = {
  23.     this
  24.   }
  25.   def map2[EE >: Nothing, B, C](b: Either[EE, B])(f: (A, B) => C): Either[EE, C] = {
  26.     for {
  27.       bb<- b
  28.     } yield f(value, bb)
  29.   }
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement