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

Add a map method directly onto Resource #437

Merged
merged 6 commits into from
Dec 6, 2018
Merged
Show file tree
Hide file tree
Changes from 3 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
10 changes: 10 additions & 0 deletions core/shared/src/main/scala/cats/effect/Resource.scala
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,16 @@ sealed abstract class Resource[F[_], A] {
def flatMap[B](f: A => Resource[F, B]): Resource[F, B] =
Bind(this, f)

/**
* Direct implementation of the `map` operation which is otherwise
* available via the `cats.Monad` instance for `Resource[F, ?]`
*
* This allows IDEs which have issues with Partial Unification to
* see `map`.
Copy link
Member

@alexandru alexandru Dec 3, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason for why this map is provided shouldn't be exposed in ScalaDoc, especially since now this implementation becomes the "source of truth" for Resource.

Just do something generic like ...

Given a mapping function, transforms the source.

This is the standard `Functor.map`.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But I think we need another ScalaDoc comment, on Resource class, mentioning:

  1. -Ypartial-unification
  2. import cats.implicits._

... to unlock all available operations.

*/
def map[B](f: A => B)(implicit F: Applicative[F]): Resource[F, B] =
flatMap(a => Resource.pure[F, B](f(a)))

/**
* Given a `Resource`, possibly built by composing multiple
* `Resource`s monadically, returns the acquired resource, as well
Expand Down
8 changes: 8 additions & 0 deletions laws/shared/src/test/scala/cats/effect/ResourceTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,13 @@ class ResourceTests extends BaseTestsSuite {
Resource.liftF(fa).use(IO.pure) <-> fa
}
}
testAsync("map should be the same as going via Monad") { implicit ec =>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of adding this test, a better idea would be to override map in the provided Monad instance.

val monad = implicitly[Monad[Resource[IO, ?]]]
check { fa: IO[String] =>
Resource.liftF(fa).map(_.toLowerCase).use(IO.pure) <->
monad.map(Resource.liftF(fa))(_.toLowerCase).use(IO.pure)
}
}

testAsync("allocated produces the same value as the resource") { implicit ec =>
check { resource: Resource[IO, Int] =>
Expand Down Expand Up @@ -117,4 +124,5 @@ class ResourceTests extends BaseTestsSuite {
val suspend = Resource.suspend[IO, Int](IO.raiseError(exception))
suspend.attempt.use(IO.pure).unsafeRunSync() shouldBe Left(exception)
}

}