-
-
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
Order for Ior #3554
Order for Ior #3554
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -708,6 +708,17 @@ sealed abstract class Ior[+A, +B] extends Product with Serializable { | |
(a, b) => that.fold(a2 => false, b2 => false, (a2, b2) => AA.eqv(a, a2) && BB.eqv(b, b2)) | ||
) | ||
|
||
final def compare[AA >: A, BB >: B](that: AA Ior BB)(implicit AA: Order[AA], BB: Order[BB]): Int = | ||
(this, that) match { | ||
case (Ior.Left(a1), Ior.Left(a2)) => AA.compare(a1, a2) | ||
case (Ior.Left(_), _) => -1 | ||
case (Ior.Both(a1, b1), Ior.Both(a2, b2)) => Order[(AA, BB)].compare((a1, b1), (a2, b2)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is kind of nitpicky but it would be pretty easy to avoid the allocation of a new There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you think it's worth it for the loss of clarity? (Genuine question, not passive-aggressive 😂 ) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looking at it again, it's actually at least three allocations (for the instance and then the tuples). Given that one of the places this could be used is for sorting collections, I'd be inclined to say three fewer object allocations per comparison is worth it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (I wouldn't block the PR for this, though.) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
case (Ior.Both(_, _), Ior.Left(_)) => 1 | ||
case (Ior.Both(_, _), Ior.Right(_)) => -1 | ||
case (Ior.Right(b1), Ior.Right(b2)) => BB.compare(b1, b2) | ||
case (Ior.Right(_), _) => 1 | ||
} | ||
|
||
final def show[AA >: A, BB >: B](implicit AA: Show[AA], BB: Show[BB]): String = | ||
fold( | ||
a => s"Ior.Left(${AA.show(a)})", | ||
|
@@ -752,9 +763,10 @@ sealed abstract private[data] class IorInstances extends IorInstances0 { | |
} | ||
} | ||
|
||
implicit def catsDataEqForIor[A: Eq, B: Eq]: Eq[A Ior B] = | ||
new Eq[A Ior B] { | ||
def eqv(x: A Ior B, y: A Ior B): Boolean = x === y | ||
implicit def catsDataOrderForIor[A: Order, B: Order]: Order[A Ior B] = | ||
new Order[A Ior B] { | ||
|
||
def compare(x: Ior[A, B], y: Ior[A, B]): Int = x.compare(y) | ||
} | ||
|
||
implicit def catsDataShowForIor[A: Show, B: Show]: Show[A Ior B] = | ||
|
@@ -879,6 +891,12 @@ sealed abstract private[data] class IorInstances0 { | |
override def map[B, C](fa: A Ior B)(f: B => C): A Ior C = | ||
fa.map(f) | ||
} | ||
|
||
implicit def catsDataEqForIor[A: Eq, B: Eq]: Eq[A Ior B] = | ||
new Eq[A Ior B] { | ||
|
||
def eqv(x: A Ior B, y: A Ior B): Boolean = x === y | ||
} | ||
} | ||
|
||
sealed private[data] trait IorFunctions { | ||
|
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 think it might be useful to have a comment with a high-level explanation of the semantics here. I think what you have makes sense as the natural way to do this, but what exactly it's doing isn't necessarily clear at a glance.
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 be honest, I copied the semantics of
===
above and didn't think too much about it! I guess it's just standard practice for a covariant datatype?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 mostly the between-ness of
Both
that I think isn't necessarily obvious. I could imagine bothLeft
andRight
precedingBoth
, for example.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.
(Sorry, by "semantics here" I meant the details of how the comparison happens, not the
AA >: A
part.)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.
Oh sorry!! Yeah I had a look in Haskell and found https://hackage.haskell.org/package/these-1.1.1.1/docs/Data-These.html. It puts
Both
afterRight
so I'm happy to change this. Although I don't know how widely used (and therefore canonical) it is. In my head, I feel likeBoth
is kind-of betweenLeft
andRight
so the ordering makes sense to me but I'm happy to be persuaded otherwise!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.
Hmm, my vote would be to follow the Haskell instance, then, but I don't have a strong opinion.
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 think there is an argument for the
These
choice in that if you think of this as an(Option, Option)
pair with twoNone
s prohibited, bothLeft
andRight
would precedeBoth
.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.
The Haskell instance is derived and I think not a product of deep thought, but I also don't see a compelling reason to diverge from prior art. I think of the type as a cleaner
Either[A, (Option[A], B)]
, whichBoth
afterRight
is consistent with.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.
Cool, I'll change it. Thanks for the feedback! :)