Either.scala 1.01 KB
Newer Older
Peter van 't Hof's avatar
Peter van 't Hof committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
package nl.lumc.sasc.biopet.utils.implicits

/**
  * Created by pjvanthof on 20/02/2017.
  */
object Either {
  implicit def optionEitherLeft[T](x: Option[Either[T,_]]): Option[Option[T]] = {
    x match {
      case Some(i:Either[T, _]) if i.isLeft => Some(i.left.toOption)
      case _ => None
    }
  }

  implicit def optionEitherRight[T](x: Option[Either[_,T]]): Option[Option[T]] = {
    x match {
      case Some(i:Either[_,T]) if i.isRight => Some(i.right.toOption)
      case _ => None
    }
  }

  implicit def eitherLeft[T](x: Either[T,_]): Option[T] = {
    x match {
      case Left(x) => Some(x)
      case _ => None
    }
  }

  implicit def eitherRight[T](x: Either[_,T]): Option[T] = {
    x match {
      case Right(x) => Some(x)
      case _ => None
    }
  }

  implicit def left[T](x:T): Left[T, _] = Left(x)
  implicit def right[T](x:T): Right[_, T] = Right(x)
  implicit def left[T](x:Option[T]): Option[Left[T, _]] = x.map(Left(_))
  implicit def right[T](x:Option[T]): Option[Right[_, T]] = x.map(Right(_))

}