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

Renamed parZip to both for consistency #1651

Merged
merged 1 commit into from
Feb 8, 2021
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
Original file line number Diff line number Diff line change
Expand Up @@ -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) = {
Expand All @@ -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]
Expand Down Expand Up @@ -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]].
Expand Down
12 changes: 6 additions & 6 deletions tests/shared/src/test/scala/cats/effect/ResourceSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down