Skip to content

Commit

Permalink
Add tapWithF to Kleisli
Browse files Browse the repository at this point in the history
  • Loading branch information
Marco Di Paola committed May 13, 2018
1 parent 680a517 commit 045c101
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
3 changes: 3 additions & 0 deletions core/src/main/scala/cats/data/Kleisli.scala
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ final case class Kleisli[F[_], A, B](run: A => F[B]) { self =>
def tapWith[C](f: (A, B) => C)(implicit F: Functor[F]): Kleisli[F, A, C] =
Kleisli(a => F.map(run(a))(b => f(a, b)))

def tapWithF[C](f: (A, B) => F[C])(implicit F: FlatMap[F]): Kleisli[F, A, C] =
Kleisli(a => F.flatMap(run(a))(b => f(a, b)))

def toReader: Reader[A, F[B]] = Kleisli[Id, A, F[B]](run)

def apply(a: A): F[B] = run(a)
Expand Down
6 changes: 6 additions & 0 deletions tests/src/test/scala/cats/tests/KleisliSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,12 @@ class KleisliSuite extends CatsSuite {
}
}

test("tapWithF") {
forAll { (f: Kleisli[List, Int, String], g: (Int, String) => List[Boolean], i: Int) =>
f.run(i).flatMap(s => g(i, s)) should === (f.tapWithF(g).run(i))
}
}

test("apply") {
forAll { (f: Kleisli[List, Int, Int], i: Int) =>
f.run(i) should === (f(i))
Expand Down

0 comments on commit 045c101

Please sign in to comment.