Created
April 12, 2021 02:39
-
-
Save hiramekun/53fa3d8fcc353175a3e6c3b7864f0d7d to your computer and use it in GitHub Desktop.
MyOrderingNum
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
case class MyOrderedNum(value: Int) extends Ordered[MyOrderedNum] { | |
override def compare(that: MyOrderedNum): Int = { | |
if (this.value < that.value) -1 | |
else if (that.value < this.value) 1 | |
else 0 | |
} | |
} | |
case class MyOrderingNum(value: Int)(implicit ord: Ordering[MyOrderingNum]) { | |
def >(that: MyOrderingNum): Boolean = ord.compare(this, that) > 0 | |
} | |
object MyNumExample extends App { | |
implicit object MyOrdering extends Ordering[MyOrderingNum] { | |
override def compare(x: MyOrderingNum, y: MyOrderingNum): Int = { | |
if (x.value < y.value) -1 | |
else if (y.value < x.value) 1 | |
else 0 | |
} | |
} | |
assert(MyOrderedNum(10) > MyOrderedNum(2)) | |
assert(MyOrderingNum(10) > MyOrderingNum(2)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment