-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
}) | ||
|
||
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -434,4 +434,22 @@ class EitherTTests extends CatsSuite { | |
} yield s1 ++ s2 | ||
} | ||
|
||
test("leftFlatMap") { | ||
forAll { (eithert: EitherT[List, String, Int], f: String => String) => | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we name this one "leftFlatMap consistent with leftMap"? |
||
eithert.leftFlatMap(v => EitherT.left[Int](List(f(v)))) should ===(eithert.leftMap(f)) | ||
} | ||
forAll { (eithert: EitherT[List, String, Int], f: String => EitherT[List, String, Int]) => | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How about naming this one "leftFlatMap consistent with swap and then flatMap"? |
||
eithert.leftFlatMap(f) should ===(eithert.swap.flatMap(a => f(a).swap).swap) | ||
} | ||
} | ||
|
||
test("leftSemiflatMap") { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above for these two tests :) |
||
forAll { (eithert: EitherT[List, String, Int], f: String => String) => | ||
eithert.leftSemiflatMap(v => List(f(v))) should ===(eithert.leftMap(f)) | ||
} | ||
forAll { (eithert: EitherT[List, String, Int], f: String => List[String]) => | ||
eithert.leftSemiflatMap(f) should ===(eithert.swap.semiflatMap(a => f(a)).swap) | ||
} | ||
} | ||
|
||
} |
There was a problem hiding this comment.
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?There was a problem hiding this comment.
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.htmlI could have sworn there were some cases where
:
was allowed and ADT patmat wasn't but I can't remember.There was a problem hiding this comment.
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 anisInstanceOf
check whereascase Type(_)
would be turned intoType.unapply(...)
, but that article shows that my assumption was false.It seems then, there's no real benefit to my suggestion, so disregard.