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 EitherT.cond #1481

Merged
merged 1 commit into from
Dec 16, 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
22 changes: 22 additions & 0 deletions core/src/main/scala/cats/data/EitherT.scala
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,28 @@ private[data] trait EitherTFunctions {
def apply[E, A](opt: Option[A], ifNone: => E)(implicit F: Applicative[F]): EitherT[F, E, A] =
EitherT(F.pure(Either.fromOption(opt, ifNone)))
}

/** 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`.
*
* {{{
* scala> import cats.Id
* scala> import cats.data.EitherT
* scala> val userInput = "hello world"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need to import cats.data.EitherT and (for the Applicative[Future]) cats.implicits._, scala.concurrent.Future and an ExecutionContext. Newlines also need a | at the start of the line (see for example the toNested example).

It seems that this cond example wasn't tested by travis (if I haven't missed it), maybe because the format wasn't right ?

The easiest way I found to quickly test a doctest example is to paste it in a fresh console (otherwise it is easy to forget some of the imports).

Apart of the example 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had the doc example code running correctly, but the travis build was spitting out errors that the imports weren't needed. I actually went back and removed the imports just for the sake of appeasing Travis!

I'll push a new commit to see what happens. If it all goes well, I can squash before a merge.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I pushed a new commit, but when I run the tests locally I get:

> test:compileIncremental
[info] Compiling 26 Scala sources to /Users/andy/git/cats/core/.jvm/target/scala-2.12/test-classes...
[error] /Users/andy/git/cats/core/.jvm/target/scala-2.12/src_managed/test/cats/data/EitherTDoctest.scala:217: Unused import
[error]     import cats.data.EitherT
[error]                      ^
[error] /Users/andy/git/cats/core/.jvm/target/scala-2.12/src_managed/test/cats/data/EitherTDoctest.scala:219: Unused import
[error]     import cats.implicits._
[error]                           ^
[error] /Users/andy/git/cats/core/.jvm/target/scala-2.12/src_managed/test/cats/data/EitherTDoctest.scala:221: Unused import
[error]     import scala.concurrent.Future
[error]                             ^
[error] /Users/andy/git/cats/core/.jvm/target/scala-2.12/src_managed/test/cats/data/EitherTDoctest.scala:223: Unused import
[error]     import scala.concurrent.ExecutionContext.Implicits.global
[error]                                                        ^
[error] four errors found
[error] (coreJVM/test:compileIncremental) Compilation failed
[error] Total time: 12 s, completed Dec 6, 2016 9:09:21 AM
>

* scala> EitherT.cond[Id](
* | userInput.forall(_.isDigit) && userInput.size == 10,
* | userInput,
* | "The input does not look like a phone number")
* res0: EitherT[Id, String, String] = EitherT(Left(The input does not look like a phone number))
* }}}
*/
final def cond[F[_]]: CondPartiallyApplied[F] = new CondPartiallyApplied

final class CondPartiallyApplied[F[_]] private[EitherTFunctions] {
def apply[E, A](test: Boolean, right: => A, left: => E)(implicit F: Applicative[F]): EitherT[F, E, A] =
EitherT(F.pure(Either.cond(test, right, left)))
}
}

private[data] abstract class EitherTInstances extends EitherTInstances1 {
Expand Down
6 changes: 6 additions & 0 deletions tests/src/test/scala/cats/tests/EitherTTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,12 @@ class EitherTTests extends CatsSuite {
}
}

test("cond") {
forAll { (cond: Boolean, s: String, i: Int) =>
Either.cond(cond, s, i) should === (EitherT.cond[Id](cond, s, i).value)
}
}

test("isLeft negation of isRight") {
forAll { (eithert: EitherT[List, String, Int]) =>
eithert.isLeft should === (eithert.isRight.map(! _))
Expand Down