Skip to content

Commit

Permalink
Backport typelevel#574: Add Resource.liftK
Browse files Browse the repository at this point in the history
  • Loading branch information
rossabaker committed Jul 30, 2019
1 parent d4427b4 commit 5255e10
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
6 changes: 6 additions & 0 deletions core/shared/src/main/scala/cats/effect/Resource.scala
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,12 @@ object Resource extends ResourceInstances with ResourcePlatform {
def liftF[F[_], A](fa: F[A])(implicit F: Applicative[F]): Resource[F, A] =
Resource.suspend(fa.map(a => Resource.pure(a)))

/**
* Lifts an applicative into a resource as a `FunctionK`. The resource has a no-op release.
*/
def liftK[F[_]](implicit F: Applicative[F]): F ~> Resource[F, ?] =
λ[F ~> Resource[F, ?]](Resource.liftF(_))

/**
* Creates a [[Resource]] by wrapping a Java
* [[https://docs.oracle.com/javase/8/docs/api/java/lang/AutoCloseable.html AutoCloseable]].
Expand Down
6 changes: 6 additions & 0 deletions laws/shared/src/test/scala/cats/effect/ResourceTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,12 @@ class ResourceTests extends BaseTestsSuite {
res.value shouldBe Some(Success(ExitCase.Canceled))
}

testAsync("liftF(fa) <-> liftK.apply(fa)") { implicit ec =>
check { (fa: IO[String], f: String => IO[Int]) =>
Resource.liftF(fa).use(f) <-> Resource.liftK[IO].apply(fa).use(f)
}
}

testAsync("evalMap") { implicit ec =>
check { (f: Int => IO[Int]) =>
Resource.liftF(IO(0)).evalMap(f).use(IO.pure) <-> f(0)
Expand Down

0 comments on commit 5255e10

Please sign in to comment.