Skip to content

Commit

Permalink
Add transform and subflatMap to OptionT and XorT
Browse files Browse the repository at this point in the history
  • Loading branch information
liff committed Nov 4, 2015
1 parent 8e1214d commit 723b53c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
6 changes: 6 additions & 0 deletions core/src/main/scala/cats/data/OptionT.scala
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ final case class OptionT[F[_], A](value: F[Option[A]]) {
case None => F.pure(None)
})

def transform[B](f: Option[A] => Option[B])(implicit F: Functor[F]): OptionT[F, B] =
OptionT(F.map(value)(f))

def subflatMap[B](f: A => Option[B])(implicit F: Functor[F]): OptionT[F, B] =
transform(_.flatMap(f))

def getOrElse(default: => A)(implicit F: Functor[F]): F[A] =
F.map(value)(_.getOrElse(default))

Expand Down
6 changes: 6 additions & 0 deletions core/src/main/scala/cats/data/XorT.scala
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ case class XorT[F[_], A, B](value: F[A Xor B]) {
def flatMapF[AA >: A, D](f: B => F[AA Xor D])(implicit F: Monad[F]): XorT[F, AA, D] =
flatMap(f andThen XorT.apply)

def transform[C, D](f: Xor[A, B] => Xor[C, D])(implicit F: Functor[F]): XorT[F, C, D] =
XorT(F.map(value)(f))

def subflatMap[AA >: A, D](f: B => AA Xor D)(implicit F: Functor[F]): XorT[F, AA, D] =
transform(_.flatMap(f))

def map[D](f: B => D)(implicit F: Functor[F]): XorT[F, A, D] = bimap(identity, f)

def leftMap[C](f: A => C)(implicit F: Functor[F]): XorT[F, C, B] = bimap(f, identity)
Expand Down

0 comments on commit 723b53c

Please sign in to comment.