Skip to content

Commit

Permalink
Revert "Add EitherT and IorT constructors from Option with monadic de…
Browse files Browse the repository at this point in the history
…fault left value (#3426)" (#3453)

This reverts commit ba44484.
  • Loading branch information
LukaJCB committed Jun 3, 2020
1 parent ba44484 commit 1f8cf3c
Show file tree
Hide file tree
Showing 10 changed files with 2 additions and 60 deletions.
9 changes: 0 additions & 9 deletions core/src/main/scala/cats/data/EitherT.scala
Expand Up @@ -838,15 +838,6 @@ object EitherT extends EitherTInstances {
}
)

/** Similar to `fromOptionF` but the left is carried from monadic `F[_]` context when the option is `None` */
final def fromOptionM[F[_], E, A](fopt: F[Option[A]], ifNone: => F[E])(implicit F: Monad[F]): EitherT[F, E, A] =
EitherT(
F.flatMap(fopt) {
case Some(a) => F.pure(Right.apply[E, A](a))
case None => F.map(ifNone)(Left.apply[E, A])
}
)

/** If the condition is satisfied, return the given `A` in `Right`
* lifted into the specified `Applicative`, otherwise, return the
* given `E` in `Left` lifted into the specified `Applicative`.
Expand Down
9 changes: 0 additions & 9 deletions core/src/main/scala/cats/data/IorT.scala
Expand Up @@ -359,15 +359,6 @@ object IorT extends IorTInstances {
final def fromOptionF[F[_], E, A](foption: F[Option[A]], ifNone: => E)(implicit F: Functor[F]): IorT[F, E, A] =
IorT(F.map(foption)(_.fold[Ior[E, A]](Ior.left(ifNone))(Ior.right)))

/** Similar to `fromOptionF` but the left is carried from monadic `F[_]` context when the option is `None` */
final def fromOptionM[F[_], E, A](foption: F[Option[A]], ifNone: => F[E])(implicit F: Monad[F]): IorT[F, E, A] =
IorT(
F.flatMap(foption) {
case Some(a) => F.pure(Ior.right[E, A](a))
case None => F.map(ifNone)(Ior.left[E, A])
}
)

/**
* Uses the [[http://typelevel.org/cats/guidelines.html#partially-applied-type-params Partially Applied Type Params technique]] for ergonomics.
*/
Expand Down
6 changes: 0 additions & 6 deletions core/src/main/scala/cats/data/OptionT.scala
Expand Up @@ -136,15 +136,9 @@ final case class OptionT[F[_], A](value: F[Option[A]]) {
def toRight[L](left: => L)(implicit F: Functor[F]): EitherT[F, L, A] =
EitherT(cata(Left(left), Right.apply))

def toRightF[L](left: => F[L])(implicit F: Monad[F]): EitherT[F, L, A] =
EitherT(cataF(F.map(left)(Left.apply[L, A]), a => F.pure(Right(a))))

def toLeft[R](right: => R)(implicit F: Functor[F]): EitherT[F, A, R] =
EitherT(cata(Right(right), Left.apply))

def toLeftF[R](right: => F[R])(implicit F: Monad[F]): EitherT[F, A, R] =
EitherT(cataF(F.map(right)(Right.apply[A, R]), a => F.pure(Left(a))))

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

def compare(that: OptionT[F, A])(implicit o: Order[F[Option[A]]]): Int =
Expand Down
3 changes: 1 addition & 2 deletions docs/src/main/tut/datatypes/eithert.md
Expand Up @@ -135,15 +135,14 @@ val numberFET: EitherT[List, String, Int] = EitherT(numberFE)

An `Option[B]` or an `F[Option[B]]`, along with a default value, can be passed to
`EitherT.fromOption` and `EitherT.fromOptionF`, respectively, to produce an
`EitherT`. For `F[Option[B]]` and default `F[A]`, there is `EitherT.fromOptionM`.
`EitherT`.

```tut:book
val myOption: Option[Int] = None
val myOptionList: List[Option[Int]] = List(None, Some(2), Some(3), None, Some(5))
val myOptionET = EitherT.fromOption[Future](myOption, "option not defined")
val myOptionListET = EitherT.fromOptionF(myOptionList, "option not defined")
val myOptionListETM = EitherT.fromOptionM(myOptionList, List("option not defined"))
```

## From `ApplicativeError[F, E]` to `EitherT[F, E, A]`
Expand Down
3 changes: 1 addition & 2 deletions docs/src/main/tut/datatypes/iort.md
Expand Up @@ -223,15 +223,14 @@ val numberF: IorT[Option, String, Int] = IorT.fromEitherF(numberFEither)

An `Option[B]` or an `F[Option[B]]`, along with a default value, can be passed
to `IorT.fromOption` and `IorT.fromOptionF`, respectively, to produce an
`IorT`. For `F[Option[B]]` and default `F[A]`, there is `IorT.fromOptionM`.
`IorT`.

```tut:silent
val numberOption: Option[Int] = None
val numberFOption: List[Option[Int]] = List(None, Some(2), None, Some(5))
val number = IorT.fromOption[List](numberOption, "Not defined")
val numberF = IorT.fromOptionF(numberFOption, "Not defined")
val numberM = IorT.fromOptionM(numberFOption, List("Not defined"))
```

## Creating an `IorT[F, A, B]` from a `Boolean` test
Expand Down
1 change: 0 additions & 1 deletion docs/src/main/tut/jump_start_guide.md
Expand Up @@ -379,7 +379,6 @@ def ensureUserExists(userId: Int): EitherT[Future, BaseException, User] = {

`toRight` is pretty analogous to the method `Either.fromOption` mentioned before: just as `fromOption` built an `Either` from an `Option`, `toRight` creates an `EitherT` from an `OptionT`.
If the original `OptionT` stores `Some` value, it will be wrapped into `Right`; otherwise the value provided as the `left` parameter will be wrapped into a `Left`.
To provide the `left` value within the monad, there is corresponding `toRightF` method.

`toLeft` is `toRight`'s counterpart which wraps the `Some` value into `Left` and transforms `None` into `Right` enclosing the provided `right` value.
This is less commonly used in practice, but can serve e.g. for enforcing uniqueness checks in code.
Expand Down
1 change: 0 additions & 1 deletion docs/src/main/tut/nomenclature.md
Expand Up @@ -198,7 +198,6 @@ Here, we use `ET` to abbreviate `EitherT`; and we use `A` and `B` as type variab
| `Either[A, B] => ET[F, A, B]` | `fromEither` | `F: Applicative` |
| `Option[B] => A => ET[F, A, B]` | `fromOption` | `F: Applicative` |
| `F[Option[B]] => A => ET[F, A, B]` | `fromOptionF` | `F: Functor` |
| `F[Option[B]] => F[A] => ET[F, A, B]` | `fromOptionM` | `F: Monad` |
| `Boolean => B => A => ET[F, A, B]` | `cond` | `F: Applicative` |
| `ET[F, A, B] => (A => C) => (B => C) => F[C]` | `fold` | `F: Functor` |
| `ET[F, A, B] => ET[F, B, A]` | `swap` | `F: Functor` |
Expand Down
12 changes: 0 additions & 12 deletions tests/src/test/scala/cats/tests/EitherTSuite.scala
Expand Up @@ -237,18 +237,6 @@ class EitherTSuite extends CatsSuite {
}
}

test("fromOptionF isLeft consistent with Option isEmpty") {
forAll { (option: Option[Int], s: String) =>
EitherT.fromOptionF[Id, String, Int](option, s).isLeft should ===(option.isEmpty)
}
}

test("fromOptionM consistent with fromOptionF") {
forAll { (option: Option[Int], s: String) =>
EitherT.fromOptionM[Id, String, Int](option, s) should ===(EitherT.fromOptionF[Id, String, Int](option, s))
}
}

test("cond consistent with Either.cond") {
forAll { (cond: Boolean, s: String, i: Int) =>
EitherT.cond[Id](cond, s, i).value should ===(Either.cond(cond, s, i))
Expand Down
6 changes: 0 additions & 6 deletions tests/src/test/scala/cats/tests/IorTSuite.scala
Expand Up @@ -353,12 +353,6 @@ class IorTSuite extends CatsSuite {
}
}

test("IorT.fromOptionM consistent with IorT.fromOptionF") {
forAll { (option: Option[Int], s: String) =>
IorT.fromOptionM[Id, String, Int](option, s) should ===(IorT.fromOptionF[Id, String, Int](option, s))
}
}

test("IorT.cond isRight equals test") {
forAll { (test: Boolean, s: String, i: Int) =>
val iort = IorT.cond[Id](test, s, i)
Expand Down
12 changes: 0 additions & 12 deletions tests/src/test/scala/cats/tests/OptionTSuite.scala
Expand Up @@ -394,24 +394,12 @@ class OptionTSuite extends CatsSuite {
}
}

test("toRight and toRightF consistent") {
forAll { (o: OptionT[List, Int], s: String) =>
o.toRight(s) should ===(o.toRightF(List(s)))
}
}

test("toLeft consistent with isDefined") {
forAll { (o: OptionT[List, Int], s: String) =>
o.toLeft(s).isLeft should ===(o.isDefined)
}
}

test("toLeft and toLeftF consistent") {
forAll { (o: OptionT[List, Int], s: String) =>
o.toLeft(s) should ===(o.toLeftF(List(s)))
}
}

test("isDefined is negation of isEmpty") {
forAll { (o: OptionT[List, Int]) =>
o.isDefined should ===(o.isEmpty.map(!_))
Expand Down

0 comments on commit 1f8cf3c

Please sign in to comment.