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 FlatMap.forEffect #1537

Merged
merged 3 commits into from
Apr 12, 2017
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
27 changes: 27 additions & 0 deletions core/src/main/scala/cats/FlatMap.scala
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,33 @@ import simulacrum.typeclass
*/
def followedByEval[A, B](fa: F[A])(fb: Eval[F[B]]): F[B] = flatMap(fa)(_ => fb.value)

/** Sequentially compose two actions, discarding any value produced by the second. */
def forEffect[A, B](fa: F[A])(fb: F[B]): F[A] = flatMap(fa)(a => map(fb)(_ => a))

/** Alias for [[forEffect]]. */
@inline final def <<[A, B](fa: F[A])(fb: F[B]): F[A] = forEffect(fa)(fb)
Copy link
Contributor

Choose a reason for hiding this comment

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

This line is the only missed test coverage, I think it's fine.


/**
* Sequentially compose two actions, discarding any value produced by the second. This variant of
* [[forEffect]] also lets you define the evaluation strategy of the second action. For instance
* you can evaluate it only ''after'' the first action has finished:
*
* {{{
* scala> import cats.Eval
* scala> import cats.implicits._
* scala> var count = 0
* scala> val fa: Option[Int] = Some(3)
* scala> def fb: Option[Unit] = Some(count += 1)
* scala> fa.forEffectEval(Eval.later(fb))
* res0: Option[Int] = Some(3)
* scala> assert(count == 1)
* scala> none[Int].forEffectEval(Eval.later(fb))
* res1: Option[Int] = None
* scala> assert(count == 1)
* }}}
*/
def forEffectEval[A, B](fa: F[A])(fb: Eval[F[B]]): F[A] = flatMap(fa)(a => map(fb.value)(_ => a))

override def ap[A, B](ff: F[A => B])(fa: F[A]): F[B] =
flatMap(ff)(f => map(fa)(f))

Expand Down
3 changes: 3 additions & 0 deletions core/src/main/scala/cats/instances/tuple.scala
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@ private[instances] class FlatMapTuple2[X](s: Semigroup[X]) extends FlatMap[(X, ?
override def followedBy[A, B](a: (X, A))(b: (X, B)): (X, B) =
(s.combine(a._1, b._1), b._2)

override def forEffect[A, B](a: (X, A))(b: (X, B)): (X, A) =
(s.combine(a._1, b._1), a._2)

override def mproduct[A, B](fa: (X, A))(f: A => (X, B)): (X, (A, B)) = {
val xb = f(fa._2)
val x = s.combine(fa._1, xb._1)
Expand Down
3 changes: 3 additions & 0 deletions laws/src/main/scala/cats/laws/FlatMapLaws.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ trait FlatMapLaws[F[_]] extends ApplyLaws[F] {
def followedByConsistency[A, B](fa: F[A], fb: F[B]): IsEq[F[B]] =
F.followedBy(fa)(fb) <-> F.flatMap(fa)(_ => fb)

def forEffectConsistency[A, B](fa: F[A], fb: F[B]): IsEq[F[A]] =
F.forEffect(fa)(fb) <-> F.flatMap(fa)(a => fb.map(_ => a))

/**
* The composition of `cats.data.Kleisli` arrows is associative. This is
* analogous to [[flatMapAssociativity]].
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ trait FlatMapTests[F[_]] extends ApplyTests[F] {
"flatMap associativity" -> forAll(laws.flatMapAssociativity[A, B, C] _),
"flatMap consistent apply" -> forAll(laws.flatMapConsistentApply[A, B] _),
"followedBy consistent flatMap" -> forAll(laws.followedByConsistency[A, B] _),
"forEffect consistent flatMap" -> forAll(laws.forEffectConsistency[A, B] _),
"mproduct consistent flatMap" -> forAll(laws.mproductConsistency[A, B] _),
"tailRecM consistent flatMap" -> forAll(laws.tailRecMConsistentFlatMap[A] _))
}
Expand Down