-
-
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 {C,c}omparison to Order, fixes #1101 #1102
Changes from 1 commit
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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package cats.kernel | ||
|
||
/** ADT encoding the possible results of a comparison */ | ||
sealed abstract class Comparison extends Product with Serializable { | ||
/** The signum of this comparison */ | ||
def toInt: Int = this match { | ||
case Comparison.GreaterThan => 1 | ||
case Comparison.EqualTo => 0 | ||
case Comparison.LessThan => -1 | ||
} | ||
} | ||
|
||
object Comparison { | ||
final case object GreaterThan extends Comparison | ||
final case object EqualTo extends Comparison | ||
final case object LessThan extends Comparison | ||
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. It would probably be nice to have a |
||
|
||
implicit val catsKernelEqForComparison: Eq[Comparison] = Eq.fromUniversalEquals | ||
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. We could probably make this an |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,6 +29,17 @@ trait Order[@sp A] extends Any with PartialOrder[A] { self => | |
*/ | ||
def compare(x: A, y: A): Int | ||
|
||
/** | ||
* Like `compare`, but returns a [[cats.kernel.Comparison]] instead of an Int. | ||
* Has the benefit of being able to pattern match on, but not as performant. | ||
*/ | ||
def comparison(x: A, y: A): Comparison = { | ||
val r = compare(x, y) | ||
if (r > 0) Comparison.GreaterThan | ||
else if (r == 0) Comparison.EqualTo | ||
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. Is it OK we're using 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. Yes, when working with a known primitive (e.g. 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 agree with @ceedubs that this should probably use |
||
else Comparison.LessThan | ||
} | ||
|
||
def partialCompare(x: A, y: A): Double = compare(x, y).toDouble | ||
|
||
/** | ||
|
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.
out of curiosity, @non, do you know it the above is faster or:
It seems to me the above would be.
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.
Yeah I think having
Comparison
instances "know" their integer value (either through fields or through methods on the subtype) would be better than using pattern matching in.toInt
.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 may be different with a
val
like that, but fwiw, at one point I benchmarked having scalaz'sMaybe
implementfold
via pattern-matching in the parent vs dynamic dispatch to the child implementation and the former seemed to have a slight edge. It may be worth throwing together a benchmark if this is something that we care about. (I'm okay with it not being something that we care about, since people can always use theInt
version of compare if they really want performance.)