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

Adding biSemiflatMap to EitherT (#2269) #2274

Merged
merged 4 commits into from
Jun 11, 2018
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
3 changes: 3 additions & 0 deletions core/src/main/scala/cats/data/EitherT.scala
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@ final case class EitherT[F[_], A, B](value: F[Either[A, B]]) {
case r@Right(_) => F.pure(r.leftCast)
})

def biSemiflatMap[C, D](fa: A => F[C], fb: B => F[D])(implicit F: Monad[F]): EitherT[F, C, D] =
leftSemiflatMap(fa).semiflatMap(fb)
Copy link
Member

Choose a reason for hiding this comment

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

Maybe it would be better to implement this in a single walk over F?

If F is e.g. List, this would traverse the list twice, and we should probably not do that ;)

Other than that, I think it's worth mentioning this PR to one of the maintainers, since there hasn't been any activity from them on this or the original issue.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fair point @kubukoz , so I would refactor it to be:

def biSemiflatMap[C, D](fa: A => F[C], fb: B => F[D])(implicit F: Monad[F]): EitherT[F, C, D] =
    EitherT(F.flatMap(value) {
      case Left(a) => F.map(fa(a)) { c => Left(c) }
      case Right(b) => F.map(fb(b)) { d => Right(d) }
    })

In this case it will flatMap and map it once, instead of flatMap it twice.


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

Expand Down
18 changes: 18 additions & 0 deletions tests/src/test/scala/cats/tests/EitherTSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -471,4 +471,22 @@ class EitherTSuite extends CatsSuite {
}
}

test("biSemiflatMap consistent with leftSemiflatMap and semiFlatmap") {
forAll { (eithert: EitherT[List, String, Int], fa: String => List[Int], fb: Int => List[String]) =>
eithert.biSemiflatMap(fa, fb) should === (eithert.leftSemiflatMap(fa).semiflatMap(fb))
}
}

test("biSemiflatMap consistent with leftSemiflatMap") {
forAll { (eithert: EitherT[List, String, Int], fa: String => List[Int]) =>
eithert.biSemiflatMap(fa, List(_)) should ===(eithert.leftSemiflatMap(a => fa(a)))
}
}

test("biSemiflatMap consistent with semiflatMap") {
forAll { (eithert: EitherT[List, String, Int], fb: Int => List[String]) =>
eithert.biSemiflatMap(List(_), fb) should ===(eithert.semiflatMap(b => fb(b)))
}
}

}