Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add toNested to OptionT and XorT. #1172

Merged
merged 2 commits into from
Jul 15, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions core/src/main/scala/cats/data/OptionT.scala
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,27 @@ final case class OptionT[F[_], A](value: F[Option[A]]) {

def foldRight[B](lb: Eval[B])(f: (A, Eval[B]) => Eval[B])(implicit F: Foldable[F]): Eval[B] =
F.compose(optionInstance).foldRight(value, lb)(f)

/**
* Transform this `OptionT[F, A]` into a `[[Nested]][F, Option, A]`.
*
* An example where `toNested` can be used, is to get the `Apply.ap` function with the
* behavior from the composed `Apply` instances from `F` and `Option`, which is
* inconsistent with the behavior of the `ap` from `Monad` of `OptionT`.
*
* {{{
* scala> import cats.implicits._
* scala> import cats.data.OptionT
* scala> val ff: OptionT[List, Int => String] =
* | OptionT(List(Option(_.toString), None))
* scala> val fa: OptionT[List, Int] = OptionT(List(Option(1), Option(2)))
* scala> ff.ap(fa)
* res0: OptionT[List,String] = OptionT(List(Some(1), Some(2), None))
* scala> OptionT(ff.toNested.ap(fa.toNested).value)
* res1: OptionT[List,String] = OptionT(List(Some(1), Some(2), None, None))
* }}}
*/
def toNested: Nested[F, Option, A] = Nested(value)
}

object OptionT extends OptionTInstances {
Expand All @@ -120,8 +141,8 @@ object OptionT extends OptionTInstances {
/**
* Transforms an `Option` into an `OptionT`, lifted into the specified `Applicative`.
*
* Note: The return type is a FromOptionPartiallyApplied[F], which has an apply method
* on it, allowing you to call fromOption like this:
* Note: The return type is a `FromOptionPartiallyApplied[F]`, which has an apply method
* on it, allowing you to call `fromOption` like this:
* {{{
* scala> import cats.implicits._
* scala> val o: Option[Int] = Some(2)
Expand All @@ -140,7 +161,6 @@ object OptionT extends OptionTInstances {

/**
* Lifts the `F[A]` Functor into an `OptionT[F, A]`.
*
*/
def liftF[F[_], A](fa: F[A])(implicit F: Functor[F]): OptionT[F, A] = OptionT(F.map(fa)(Some(_)))
}
Expand Down
28 changes: 28 additions & 0 deletions core/src/main/scala/cats/data/XorT.scala
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,34 @@ final case class XorT[F[_], A, B](value: F[A Xor B]) {
XorT(F.map(value)(xor => f(xor.toValidated).toXor))

def show(implicit show: Show[F[A Xor B]]): String = show.show(value)

/**
* Transform this `XorT[F, A, B]` into a `[[Nested]][F, A Xor ?, B]`.
*
* An example where `toNested` can be used, is to get the `Apply.ap` function with the
* behavior from the composed `Apply` instances from `F` and `Xor[A, ?]`, which is
* inconsistent with the behavior of the `ap` from `Monad` of `XorT`.
*
* {{{
* scala> import cats.data.{Nested, Xor, XorT}
* scala> import cats.implicits._
* scala> val ff: XorT[List, String, Int => String] =
* | XorT(List(Xor.right(_.toString), Xor.left("error")))
* scala> val fa: XorT[List, String, Int] =
* | XorT(List(Xor.right(1), Xor.right(2)))
* scala> type ErrorOr[A] = String Xor A
* scala> type ListErrorOr[A] = Nested[List, ErrorOr, A]
* scala> ff.ap(fa)
* res0: XorT[List,String,String] = XorT(List(Right(1), Right(2), Left(error)))
* scala> XorT((ff.toNested: ListErrorOr[Int => String]).ap(fa.toNested: ListErrorOr[Int]).value)
* res1: XorT[List,String,String] = XorT(List(Right(1), Right(2), Left(error), Left(error)))
* }}}
*
* Note that we need the `ErrorOr` type alias above because otherwise we can't use the
* syntax function `ap` on `Nested[List, A Xor ?, B]`. This won't be needed after cats has
* decided [[https://github.com/typelevel/cats/issues/1073 how to handle the SI-2712 fix]].
*/
def toNested: Nested[F, A Xor ?, B] = Nested[F, A Xor ?, B](value)
}

object XorT extends XorTInstances with XorTFunctions
Expand Down