Skip to content

Commit

Permalink
Add FlatMap#flatTap, a more principled version of the kestrel combina…
Browse files Browse the repository at this point in the history
…tor.

It's a useful way of adding side-effects (as in, effects on the side)
of a monadic (flatMappish?) computation.
  • Loading branch information
hrhino committed Oct 10, 2017
1 parent b84015a commit 3757d7a
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions core/src/main/scala/cats/FlatMap.scala
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,24 @@ import simulacrum.typeclass
* Implementations of this method should use constant stack space relative to `f`.
*/
def tailRecM[A, B](a: A)(f: A => F[Either[A, B]]): F[B]

/**
* Apply a monadic function and discard the result while keeping the effect.
*
* {{{
* scala> import cats._, implicits._
* scala> Option(1).flatTap(_ => None)
* res0: Option[Int] = None
* scala> Option(1).flatTap(_ => Some("123"))
* res1: Option[Int] = Some(1)
* scala> def nCats(n: Int) = List.fill(n)("cat")
* nCats: (n: Int)List[String]
* scala> List[Int](0).flatTap(nCats)
* res2: List[Int] = List()
* scala> List[Int](4).flatTap(nCats)
* res3: List[Int] = List(4, 4, 4, 4)
* }}}
*/
def flatTap[A, B](fa: F[A])(f: A => F[B]): F[A] =
flatMap(fa)(a => map(f(a))(_ => a))
}

0 comments on commit 3757d7a

Please sign in to comment.