Created
January 4, 2014 02:54
-
-
Save kuzuha/8250907 to your computer and use it in GitHub Desktop.
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
import Ordering.Implicits._ | |
case class Version(name: String, numbers: (Int, Int, Int, Int)) extends AnyRef with Ordered[Version] { | |
def compare(other: Version) = if (numbers == other.numbers) 0 else if (numbers < other.numbers) -1 else 1 | |
} |
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
import org.junit.runner.RunWith | |
import org.specs2.runner.JUnitRunner | |
import org.specs2.mutable.Specification | |
@RunWith(classOf[JUnitRunner]) | |
class VersionSpec extends Specification { | |
"1.0.0" should { | |
"less than 1.0.1" in { | |
Version("1.0.0", (1, 0, 0, 0)) < Version("1.0.1", (1, 0, 1, 0)) must beTrue | |
} | |
"greater than 0.9.8" in { | |
Version("1.0.0", (1, 0, 0, 0)) > Version("0.9.8", (0, 9, 8, 0)) must beTrue | |
} | |
} | |
} |
以下のようになら書けるけど、ちょっと微妙・・・
case class Version(name: String, numbers: (Int, Int, Int, Int)) extends AnyRef with Ordered[Version] {
def compare(other: Version) = Ordering[Tuple4[Int,Int,Int,Int]].compare(numbers, other.numbers)
}
Ordering.Ops
にcompare
があれば、@j5ik2oさんが言ったように書けるのに、なんでないんでしょうね・・・
https://github.com/scala/scala/blob/v2.10.3/src/library/scala/math/Ordering.scala#L126-L134
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
numbers compare other.numbers