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

Conversation

andimiller
Copy link

This enables it to be found by IDEs which have issues with partial
unification and feels like it should be there since the class already
provides flatMap.

This enables it to be found by IDEs which have issues with partial
unification and feels like it should be there since the class already
provides `flatMap`.
* see `map`.
*/
def map[B](f: A => B)(implicit F: Monad[F]): Resource[F, B] =
flatMap(a => Resource.applyCase(F.pure((f(a), _ => F.unit))))
Copy link
Contributor

Choose a reason for hiding this comment

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

I think you can just use Resource.pure instead of the manual apply case (or conversely just build the tree using Bind and Allocate). In any case the constraint on Fcan probably be weakened to Applicative

Copy link
Author

Choose a reason for hiding this comment

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

Good catch, I got slightly hung up on following how map was implemented via Monad and ended up using applyCase, I've simplified it to using pure and weakened the F to Applicative

@alexandru
Copy link
Member

alexandru commented Dec 3, 2018

Speaking of this, somebody should also start a ticket with JetBrains for IntelliJ IDEA, maybe somebody ends up fixing their support. With precisely this example, map usage on Resource.

It's been worsening lately, not sure what they are doing, probably rebuilding their engine or something.

@@ -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.

@codecov-io
Copy link

codecov-io commented Dec 3, 2018

Codecov Report

Merging #437 into master will increase coverage by 4.03%.
The diff coverage is 100%.

@@            Coverage Diff             @@
##           master     #437      +/-   ##
==========================================
+ Coverage   85.44%   89.47%   +4.03%     
==========================================
  Files          70       70              
  Lines        2102     2005      -97     
  Branches      202      209       +7     
==========================================
- Hits         1796     1794       -2     
+ Misses        306      211      -95

* 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.

@kubukoz kubukoz mentioned this pull request Dec 6, 2018
@alexandru alexandru merged commit 504c437 into typelevel:master Dec 6, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants