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

Implement EitherT#leftFlatMap and EitherT#leftSemiflatMap #1790

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
12 changes: 12 additions & 0 deletions core/src/main/scala/cats/data/EitherT.scala
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,18 @@ final case class EitherT[F[_], A, B](value: F[Either[A, B]]) {

def leftMap[C](f: A => C)(implicit F: Functor[F]): EitherT[F, C, B] = bimap(f, identity)

def leftFlatMap[BB >: B, D](f: A => EitherT[F, D, BB])(implicit F: Monad[F]): EitherT[F, D, BB] =
EitherT(F.flatMap(value) {
case Left(a) => f(a).value
case r@Right(_) => F.pure(r.leftCast)
Copy link
Member

Choose a reason for hiding this comment

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

Tiny nitpick, but can we change this to case r: Right => ... since we don't actually want to extract anything?

Copy link
Contributor

Choose a reason for hiding this comment

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

While in practice I don't think it matters, the use of : implies type casing which is generally used in contexts where parametricity is violated: https://typelevel.org/blog/2014/11/10/why_is_adt_pattern_matching_allowed.html

I could have sworn there were some cases where : was allowed and ADT patmat wasn't but I can't remember.

Copy link
Member

Choose a reason for hiding this comment

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

Very interesting article, thanks a lot! :)
I always thought that scalac transforms case t: Type into an isInstanceOf check whereas case Type(_) would be turned into Type.unapply(...), but that article shows that my assumption was false.
It seems then, there's no real benefit to my suggestion, so disregard.

})

def leftSemiflatMap[D](f: A => F[D])(implicit F: Monad[F]): EitherT[F, D, B] =
EitherT(F.flatMap(value) {
case Left(a) => F.map(f(a)) { d => Left(d) }
case r@Right(_) => F.pure(r.leftCast)
Copy link
Member

Choose a reason for hiding this comment

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

Ditto, as above :)

})

def compare(that: EitherT[F, A, B])(implicit o: Order[F[Either[A, B]]]): Int =
o.compare(value, that.value)

Expand Down
24 changes: 24 additions & 0 deletions tests/src/test/scala/cats/tests/EitherTTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -434,4 +434,28 @@ class EitherTTests extends CatsSuite {
} yield s1 ++ s2
}

test("leftFlatMap consistent with leftMap") {
forAll { (eithert: EitherT[List, String, Int], f: String => String) =>
eithert.leftFlatMap(v => EitherT.left[Int](List(f(v)))) should ===(eithert.leftMap(f))
}
}

test("leftFlatMap consistent with swap and then flatMap") {
forAll { (eithert: EitherT[List, String, Int], f: String => EitherT[List, String, Int]) =>
eithert.leftFlatMap(f) should ===(eithert.swap.flatMap(a => f(a).swap).swap)
}
}

test("leftSemiflatMap consistent with leftMap") {
forAll { (eithert: EitherT[List, String, Int], f: String => String) =>
eithert.leftSemiflatMap(v => List(f(v))) should ===(eithert.leftMap(f))
}
}

test("leftSemiflatmap consistent with swap and the semiflatMap") {
forAll { (eithert: EitherT[List, String, Int], f: String => List[String]) =>
eithert.leftSemiflatMap(f) should ===(eithert.swap.semiflatMap(a => f(a)).swap)
}
}

}