-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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 PartialFunction instance for Profunctor typeclass #3392
Add PartialFunction instance for Profunctor typeclass #3392
Conversation
*/ | ||
override def dimap[A, B, C, D](fab: PartialFunction[A, B])(f: C => A)(g: B => D): PartialFunction[C, D] = | ||
new PartialFunction[C, D] { | ||
override def isDefinedAt(c: C): Boolean = fab.isDefinedAt(f(c)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think if there's any way out of running f
twice
Codecov Report
@@ Coverage Diff @@
## master #3392 +/- ##
==========================================
- Coverage 92.70% 92.48% -0.23%
==========================================
Files 379 379
Lines 7981 7960 -21
Branches 230 226 -4
==========================================
- Hits 7399 7362 -37
- Misses 582 598 +16
Continue to review full report at Codecov.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for this! :)
I wonder if we could add instances for the other parts of the |
@LukaJCB I updated the same PR instead, is it okay? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @gagandeepkalra sorry for keeping you waiting for so long, this looks amazing, thanks! Do you have time to rebase with master? :)
ff84eea
to
6af528d
Compare
Codecov Report
@@ Coverage Diff @@
## master #3392 +/- ##
==========================================
+ Coverage 91.63% 92.05% +0.42%
==========================================
Files 382 383 +1
Lines 8304 8347 +43
Branches 232 210 -22
==========================================
+ Hits 7609 7684 +75
+ Misses 695 663 -32 |
Thank you @LukaJCB @barambani for the review, learnt something new today. |
Thank you.
same here. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey, thanks! This looks great, but I wonder why you only placed it in the companion objects of Profunctor
and Compose
, maybe I'm missing something?
Ah of course! Sorry I misremembered a detail of the whole thing |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 to the concept—I'm not sure why we hadn't done this before. I do have a couple of comments / questions about details.
@@ -41,9 +41,12 @@ import scala.annotation.implicitNotFound | |||
} | |||
|
|||
object Compose { | |||
implicit def catsInstancesForFunction1: ArrowChoice[Function1] with CommutativeArrow[Function1] = | |||
implicit val catsInstancesForFunction1: ArrowChoice[Function1] with CommutativeArrow[Function1] = |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's not really a big deal, but there is a reason for these to be methods instead of val
s: they're already instantiated as val
s in the instances
objects, so using def
here won't result in new instantiations, but using a val
does result in a little extra overhead and indirection, since it generates both an accessor method and a field.
In general this is unlikely to matter much, but because there are a lot of these, we've had a policy of def
instead of val
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see, that's a nice way of saving up on a reference; apologies for oversight
@@ -10,6 +10,7 @@ abstract class AllInstancesBinCompat | |||
with AllInstancesBinCompat4 | |||
with AllInstancesBinCompat5 | |||
with AllInstancesBinCompat6 | |||
with AllInstancesBinCompat7 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We shouldn't need any new BinCompat
traits now that we've dropped 2.11. You should be able to add PartialFunctionInstances
directly to AllInstances
, I believe?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure, getting this error when adding to AllInstances
; I don't see anything obvious, might need some help
[error] Cats core: Failed binary compatibility check against org.typelevel:cats-core_2.12:1.0.1! Found 2 potential problems (filtered 481)
[error] * abstract synthetic method cats$instances$PartialFunctionInstances$setter$catsStdInstancesForPartialFunction_=(cats.arrow.ArrowChoice)Unit in interface cats.instances.PartialFunctionInstances is inherited by class AllInstances in current version.
[error] filter with: ProblemFilters.excludeInheritedNewAbstractMethodProblem
[error] * abstract method catsStdInstancesForPartialFunction()cats.arrow.ArrowChoice in interface cats.instances.PartialFunctionInstances is inherited by class AllInstances in current version.
[error] filter with: ProblemFilters.excludeInheritedNewAbstractMethodProblem
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@gagandeepkalra Hmm, thanks, I'll take a look now.
Oh, right. The issue is that adding a |
Here's some explanation of the issue: #2789 (comment) I think that something like this should work: trait PartialFunctionInstances {
def catsStdInstancesForPartialFunction: ArrowChoice[PartialFunction] with CommutativeArrow[PartialFunction] =
PartialFunctionInstances.instance
}
private object PartialFunctionInstances {
private val instance: ArrowChoice[PartialFunction] with CommutativeArrow[PartialFunction] =
new ArrowChoice[PartialFunction] with CommutativeArrow[PartialFunction] {
// ... I'd be happy to merge this PR as-is, and I can fix this particular part in a follow-up, if you'd prefer? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's one remaining issue, but I think it could be handled in a follow-up PR, so I'm approving this now. Thanks again @gagandeepkalra!
…improved doctests with less imports now needed
@travisbrown thank you, summarising- I understand why adding a def to trait won't break bc given we have method defaults in java 8 interface, while though Meanwhile I had another doc-test import related improvement, so have pushed both together. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@gagandeepkalra Thanks, that looks good to me! I don't it necessarily needs a second re-review, but I'll leave it for right now for @LukaJCB to take a look if he wants.
I don't remember the details about why adding a val
to a trait breaks bincompat in 2.12, but right, it has something to default methods (and the implementation of initialization using static forwarder methods on the companion object).
Going ahead and merging this since it had an earlier second 👍. |
resolves #3386