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 an If-Then-Else operation to the Apply. #2609

Merged
merged 8 commits into from
Jan 12, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
8 changes: 8 additions & 0 deletions core/src/main/scala/cats/Apply.scala
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,14 @@ trait Apply[F[_]] extends Functor[F] with InvariantSemigroupal[F] with ApplyArit
def map2Eval[A, B, Z](fa: F[A], fb: Eval[F[B]])(f: (A, B) => Z): Eval[F[Z]] =
fb.map(fb => map2(fa, fb)(f))

/**
* `if-then-else` lifted into the applicative
*/
def ifA[A](cond: F[Boolean])(ifTrue: F[A], ifFalse: F[A]): F[A] = {
def ite(cond: Boolean)(t: A, f: A) = if (cond) t else f
ap2(map(cond)(ite))(ifTrue, ifFalse)
}

/**
* Compose an `Apply[F]` and an `Apply[G]` into an `Apply[λ[α => F[G[α]]]]`.
*
Expand Down
28 changes: 28 additions & 0 deletions core/src/main/scala/cats/syntax/apply.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,34 @@ trait ApplySyntax extends TupleSemigroupalSyntax {
new ApplyOps(fa)
}

final class IfAOps[F[_]](val fa: F[Boolean]) extends AnyVal {

/**
* A conditional lifted into the `F` context.
*
* Example:
* {{{
* scala> import cats.implicits._
*
* scala> val b1: Option[Boolean] = Some(true)
* scala> val asInt1: Option[Int] = b1.ifA(Some(1), Some(0))
* scala> asInt1.get
* res0: Int = 1
*
* scala> val b2: Option[Boolean] = Some(false)
* scala> val asInt2: Option[Int] = b2.ifA(Some(1), Some(0))
* scala> asInt2.get
* res1: Int = 0
*
* scala> val b3: Option[Boolean] = Some(true)
* scala> val asInt3: Option[Int] = b3.ifA(Some(1), None)
* asInt2: Option[Int] = None
*
* }}}
Copy link
Contributor

Choose a reason for hiding this comment

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

Unfortunately we can't add any methods to type classses without breaking binary compatibility. So for now, we have to settle adding to a new syntax trait. Sorry I am on my phone and couldn't point you to an 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.

@kailuowang If I understand you correctly, we can not add the method to the trait Apply[F[_]] (the type-class) but can only leave it as part of a syntactic extension implicit class?

Copy link
Contributor

Choose a reason for hiding this comment

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

sorry for the delayed response. Yes, that's the idea. You already updated the PR, we also need to create a new ApplySyntaxBinCompat trait to include an implicit conversion from F[Boolean] to IfAOps, then there are a couple of more steps,

  1. here we need to add a new trait AllSyntaxBinCompat4 to extends the new ApplySyntaxBinCompat,
    let the AllSyntaxBinCompat at the top extend this AllSyntaxBinCompat4
  2. [here] https://github.com/typelevel/cats/blob/master/core/src/main/scala/cats/implicits.scala we also need to let the implicits object extends this new AllSyntaxBinCompat4
  3. then we can probably add a doctest here that import cats.implicits._ to validate that it's all hooked up.

Sorry about the gymnastics you have to go through to overcome the Scala 2.11 binary compatibility restrictions.

*/
def ifA[A](ifTrue: F[A], ifFalse: => F[A])(implicit F: Apply[F]): F[A] = F.ifA(fa)(ifTrue, ifFalse)
}

final class ApplyOps[F[_], A](val fa: F[A]) extends AnyVal {

/** Alias for [[Apply.productR]]. */
Expand Down