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

Follow up to #3233 #3237

Merged
merged 3 commits into from
Jan 4, 2020
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
6 changes: 3 additions & 3 deletions core/src/main/scala/cats/data/OptionT.scala
Original file line number Diff line number Diff line change
Expand Up @@ -233,10 +233,10 @@ object OptionT extends OptionTInstances {

/**
* Creates a non-empty `OptionT[F, A]` from an `F[A]` value if the given condition is `true`.
* Otherwise, the empty `OptionT[F, A]` is returned. Analogous to `Option.when`.
* Otherwise, `none[F, A]` is returned. Analogous to `Option.when`.
*/
def whenF[F[_], A](cond: Boolean)(fa: => F[A])(implicit F: Applicative[F]): OptionT[F, A] =
if (cond) OptionT.liftF(fa) else OptionT(F.map(fa)(_ => Option.empty))
if (cond) OptionT.liftF(fa) else OptionT.none[F, A]

/**
* Same as `whenF`, but expressed as a FunctionK for use with mapK.
Expand All @@ -253,7 +253,7 @@ object OptionT extends OptionTInstances {

/**
* Creates an non-empty `OptionT[F, A]` from an `F[A]` if the given condition is `false`.
* Otherwise, the empty `OptionT[F, A]` is returned. Analogous to `Option.unless`.
* Otherwise, `none[F, A]` is returned. Analogous to `Option.unless`.
*/
def unlessF[F[_], A](cond: Boolean)(fa: => F[A])(implicit F: Applicative[F]): OptionT[F, A] =
OptionT.whenF(!cond)(fa)
Expand Down
28 changes: 16 additions & 12 deletions tests/src/test/scala/cats/tests/OptionTSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -278,17 +278,19 @@ class OptionTSuite extends CatsSuite {
}
}

test("OptionT.when[Id, A] consistent with the same implementation of Option.when") {
val when = (c: Boolean, j: Int) => if (c) Some(j) else None
test("OptionT.when[Id, A] consistent with Option.when") {
// Option.when is inlined here because it is not available before Scala 2.13
def when[A]: (Boolean, A) => Option[A] = (c: Boolean, a: A) => if (c) Some(a) else None
forAll { (i: Int, b: Boolean) =>
OptionT.when[Id, Int](b)(i).value should ===(when(b, i))
}
}

test("OptionT.whenF[F, A] consistent with the same implementation of Option.when") {
val when = (c: Boolean, j: Int) => if (c) Some(j) else None
forAll { (li: List[Int], b: Boolean) =>
OptionT.whenF(b)(li).value should ===(li.map(when(b, _)))
test("OptionT.whenF[Id, A] consistent with Option.when") {
// Option.when is inlined here because it is not available before Scala 2.13
def when[A]: (Boolean, A) => Option[A] = (c: Boolean, a: A) => if (c) Some(a) else None
Copy link
Member

Choose a reason for hiding this comment

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

Not a blocker, but since the property refers to Option.when, it might be helpful to future readers to comment why it's inlined here (e.g., that it's not available before 2.13).

forAll { (i: Int, b: Boolean) =>
OptionT.whenF[Id, Int](b)(i).value should ===(when(b, i))
}
}

Expand All @@ -298,17 +300,19 @@ class OptionTSuite extends CatsSuite {
}
}

test("OptionT.unless[Id, A] consistent with the same implementation of Option.unless") {
val unless = (c: Boolean, j: Int) => if (!c) Some(j) else None
test("OptionT.unless[Id, A] consistent with Option.unless") {
// Option.unless is inlined here because it is not available before Scala 2.13
def unless[A]: (Boolean, A) => Option[A] = (c: Boolean, a: A) => if (!c) Some(a) else None
forAll { (i: Int, b: Boolean) =>
OptionT.unless[Id, Int](b)(i).value should ===(unless(b, i))
}
}

test("OptionT.unlessF[F, A] consistent with the same implementation of Option.unless") {
val unless = (c: Boolean, j: Int) => if (!c) Some(j) else None
forAll { (li: List[Int], b: Boolean) =>
OptionT.unlessF(b)(li).value should ===(li.map(unless(b, _)))
test("OptionT.unlessF[Id, A] consistent with Option.unless") {
// Option.unless is inlined here because it is not available before Scala 2.13
def unless[A]: (Boolean, A) => Option[A] = (c: Boolean, a: A) => if (!c) Some(a) else None
forAll { (i: Int, b: Boolean) =>
OptionT.unlessF[Id, Int](b)(i).value should ===(unless(b, i))
}
}

Expand Down