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 semiflatTap and leftSemiflatTap functions to EitherT #3316

Merged
merged 2 commits into from
Feb 27, 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: 6 additions & 0 deletions core/src/main/scala/cats/data/EitherT.scala
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,12 @@ final case class EitherT[F[_], A, B](value: F[Either[A, B]]) {
}
})

def semiflatTap[C](f: B => F[C])(implicit F: Monad[F]): EitherT[F, A, B] =
semiflatMap(b => F.as(f(b), b))

def leftSemiflatTap[C](f: A => F[C])(implicit F: Monad[F]): EitherT[F, A, B] =
Copy link
Member

Choose a reason for hiding this comment

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

What do we think about using Unit here to signify that this resulting type is going to be discarded? Might be a bit more cumbersome on the usage side, but it's just an extra .void

Copy link
Contributor

@travisbrown travisbrown Feb 24, 2020

Choose a reason for hiding this comment

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

We already have a flatTap on FlatMap that doesn't require Unit, and tap, tapWith, etc. on Kleisli don't constrain the output type to be Unit. My personal preference would be consistency—I think that plus slightly easier use outweighs a little extra expressivity in the types.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@LukaJCB I first thought about it, but then saw that FlatMap[F].flatTap takes A => F[B] instead of A => F[Unit], so I wanted to be consistent with existing signature.

leftSemiflatMap(a => F.as(f(a), a))

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

Expand Down
42 changes: 39 additions & 3 deletions tests/src/test/scala/cats/tests/EitherTSuite.scala
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package cats
package tests

import cats.Bifunctor
import cats.data.EitherT

import cats.data.{EitherT, State}
import cats.laws.discipline._
import cats.laws.discipline.arbitrary._
import cats.laws.discipline.SemigroupalTests.Isomorphisms
import cats.kernel.laws.discipline.{EqTests, MonoidTests, OrderTests, PartialOrderTests, SemigroupTests}

import scala.util.{Failure, Success, Try}

class EitherTSuite extends CatsSuite {
Expand Down Expand Up @@ -541,6 +540,43 @@ class EitherTSuite extends CatsSuite {
}
}

test("semiflatTap does not change the return value") {
type TestEffect[A] = State[List[Int], A]
forAll { (eithert: EitherT[TestEffect, String, Int], f: Int => TestEffect[Int], initial: List[Int]) =>
eithert.semiflatTap(v => f(v)).value.runA(initial) should ===(eithert.value.runA(initial))
}
}

test("semiflatTap runs the effect") {
type TestEffect[A] = State[List[Int], A]
forAll { (eithert: EitherT[TestEffect, String, Int], f: Int => TestEffect[Int], initial: List[Int]) =>
eithert.semiflatTap(v => f(v)).value.runS(initial) should ===(eithert.semiflatMap(f).value.runS(initial))
}
}

test("leftSemiflatTap does not change the return value") {
type TestEffect[A] = State[List[Int], A]
forAll { (eithert: EitherT[TestEffect, String, Int], f: String => TestEffect[Int], initial: List[Int]) =>
eithert.leftSemiflatTap(v => f(v)).value.runA(initial) should ===(eithert.value.runA(initial))
}
}

test("leftSemiflatTap runs the effect") {
type TestEffect[A] = State[List[Int], A]
forAll { (eithert: EitherT[TestEffect, String, Int], f: String => TestEffect[Int], initial: List[Int]) =>
eithert.leftSemiflatTap(v => f(v)).value.runS(initial) should ===(eithert.leftSemiflatMap(f).value.runS(initial))
}
}

test("leftSemiflatTap consistent with swap and the semiflatTap") {
type TestEffect[A] = State[List[Int], A]
forAll { (eithert: EitherT[TestEffect, String, Int], f: String => TestEffect[Int], initial: List[Int]) =>
eithert.leftSemiflatTap(v => f(v)).value.runA(initial) should ===(
eithert.swap.semiflatTap(v => f(v)).swap.value.runA(initial)
)
}
}

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))
Expand Down