Skip to content

Instantly share code, notes, and snippets.

@kuzuha
Created January 4, 2014 02:54
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kuzuha/8250907 to your computer and use it in GitHub Desktop.
Save kuzuha/8250907 to your computer and use it in GitHub Desktop.
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
}
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
}
}
}
@j5ik2o
Copy link

j5ik2o commented Jan 4, 2014

numbers compare other.numbers

@xuwei-k
Copy link

xuwei-k commented Jan 4, 2014

以下のようになら書けるけど、ちょっと微妙・・・

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.Opscompareがあれば、@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