Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- sealed trait Either[+E, +A] {
- def map[B](f: A => B): Either[E, B]
- def flatMap[EE >: E, B](f: A => Either[EE, B]): Either[EE, B]
- def orElse[EE >: E,B >: A](b: => Either[EE, B]): Either[EE, B]
- def map2[EE >: E, B, C](b: Either[EE, B])(f: (A, B) => C): Either[EE, C]
- }
- case class Left[+E](value: E) extends Either[E, Nothing] {
- def map[B](f: (Nothing) => B): Either[E, B] = this
- def flatMap[EE >: E, B](f: Nothing => Either[EE, B]): Either[EE, B] = this
- def orElse[EE >: E,B >: Nothing](b: => Either[EE, B]): Either[EE, B] = b
- def map2[EE >: E, B, C](b: Either[EE, B])(f: (Nothing, B) => C): Either[EE, C] = this
- }
- case class Right[+A](value: A) extends Either[Nothing, A] {
- def map[B](f: A => B): Either[Nothing, B] = {
- Right(f(value))
- }
- def flatMap[EE >: Nothing, B](f: A => Either[EE, B]): Either[EE, B] = {
- f(value)
- }
- def orElse[EE >: Nothing,B >: A](b: => Either[EE, B]): Either[EE, B] = {
- this
- }
- def map2[EE >: Nothing, B, C](b: Either[EE, B])(f: (A, B) => C): Either[EE, C] = {
- for {
- bb<- b
- } yield f(value, bb)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement