From d0033cd145e60741bf4aafbc1d61cac5f6a33e87 Mon Sep 17 00:00:00 2001 From: Daniel Spiewak Date: Sat, 6 Feb 2021 23:47:59 -0700 Subject: [PATCH] Renamed parZip to both for consistency --- .../src/main/scala/cats/effect/kernel/Resource.scala | 9 +++++++-- .../src/test/scala/cats/effect/ResourceSpec.scala | 12 ++++++------ 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/kernel/shared/src/main/scala/cats/effect/kernel/Resource.scala b/kernel/shared/src/main/scala/cats/effect/kernel/Resource.scala index b65e005875..88a658446f 100644 --- a/kernel/shared/src/main/scala/cats/effect/kernel/Resource.scala +++ b/kernel/shared/src/main/scala/cats/effect/kernel/Resource.scala @@ -251,7 +251,7 @@ sealed abstract class Resource[F[_], +A] { * * Note that `Resource` also comes with a `cats.Parallel` instance * that offers more convenient access to the same functionality as - * `parZip`, for example via `parMapN`: + * `both`, for example via `parMapN`: * * {{{ * def mkResource(name: String) = { @@ -268,7 +268,7 @@ sealed abstract class Resource[F[_], +A] { * .use(msg => IO(println(msg))) * }}} */ - def parZip[B]( + def both[B]( that: Resource[F, B] )(implicit F: Concurrent[F]): Resource[F, (A, B)] = { type Update = (F[Unit] => F[Unit]) => F[Unit] @@ -654,6 +654,11 @@ object Resource extends ResourceFOInstances0 with ResourceHOInstances0 with Reso def apply[A](fa: F[A]): Resource[F, A] = Resource.eval(fa) } + def both[F[_]: Concurrent, A, B]( + rfa: Resource[F, A], + rfb: Resource[F, B]): Resource[F, (A, B)] = + rfa.both(rfb) + /** * Creates a [[Resource]] by wrapping a Java * [[https://docs.oracle.com/javase/8/docs/api/java/lang/AutoCloseable.html AutoCloseable]]. diff --git a/tests/shared/src/test/scala/cats/effect/ResourceSpec.scala b/tests/shared/src/test/scala/cats/effect/ResourceSpec.scala index 1f849247e8..1a6a4d3dca 100644 --- a/tests/shared/src/test/scala/cats/effect/ResourceSpec.scala +++ b/tests/shared/src/test/scala/cats/effect/ResourceSpec.scala @@ -400,10 +400,10 @@ class ResourceSpec extends BaseSpec with ScalaCheck with Discipline { suspend.use_ must failAs(exception) } - "parZip" >> { + "both" >> { "releases resources in reverse order of acquisition" in ticked { implicit ticker => // conceptually asserts that: - // forAll (r: Resource[F, A]) then r <-> r.parZip(Resource.unit) <-> Resource.unit.parZip(r) + // forAll (r: Resource[F, A]) then r <-> r.both(Resource.unit) <-> Resource.unit.both(r) // needs to be tested manually to assert the equivalence during cleanup as well forAll { (as: List[(Int, Either[Throwable, Unit])], rhs: Boolean) => var released: List[Int] = Nil @@ -412,7 +412,7 @@ class ResourceSpec extends BaseSpec with ScalaCheck with Discipline { Resource.make(IO(a))(a => IO { released = a :: released } *> IO.fromEither(e)) } val unit = ().pure[Resource[IO, *]] - val p = if (rhs) r.parZip(unit) else unit.parZip(r) + val p = if (rhs) r.both(unit) else unit.both(r) p.use_.attempt.void must completeAs(()) released mustEqual as.map(_._1) @@ -435,7 +435,7 @@ class ResourceSpec extends BaseSpec with ScalaCheck with Discipline { IO { rightReleasing = true } >> wait >> IO { rightReleased = true } } - lhs.parZip(rhs).use(_ => wait).unsafeToFuture() + lhs.both(rhs).use(_ => wait).unsafeToFuture() // after 1 second: // both resources have allocated (concurrency, serially it would happen after 2 seconds) @@ -484,7 +484,7 @@ class ResourceSpec extends BaseSpec with ScalaCheck with Discipline { _ <- Resource.eval(wait(2)) } yield () - lhs.parZip(rhs).use(_ => IO.unit).handleError(_ => ()).unsafeToFuture() + lhs.both(rhs).use(_ => IO.unit).handleError(_ => ()).unsafeToFuture() // after 1 second: // both resources have allocated (concurrency, serially it would happen after 2 seconds) @@ -532,7 +532,7 @@ class ResourceSpec extends BaseSpec with ScalaCheck with Discipline { IO.unit) } yield () - lhs.parZip(rhs).use(_ => wait(1)).handleError(_ => ()).unsafeToFuture() + lhs.both(rhs).use(_ => wait(1)).handleError(_ => ()).unsafeToFuture() // after 1 second: // rhs has partially allocated, lhs executing