Skip to content

Instantly share code, notes, and snippets.

@gkojax
Created August 5, 2012 08:37
Show Gist options
  • Save gkojax/3262982 to your computer and use it in GitHub Desktop.
Save gkojax/3262982 to your computer and use it in GitHub Desktop.
Dateに対するOrderのインスタンス無くても動くらしい
import scalaz._, Scalaz._
import java.util.Date
case class Student(name: String, grade: Int, birthday: Date)
object Student {
implicit object StudentInstance extends Show[Student] with Order[Student] {
def show(s: Student) = s.toString.toList
def order(s1: Student, s2: Student) =
s1.grade ?|? s2.grade |+| s1.birthday ?|? s2.birthday
}
}
val format = new java.text.SimpleDateFormat("yyyy MM dd")
val akari = Student("akari", 1, format.parse("1995 07 24"))
val kyoko = Student("kyoko", 2, format.parse("1995 03 28"))
val yui = Student("yui", 2, format.parse("1994 04 22"))
val chinatsu = Student("chinatsu", 1, format.parse("1995 11 06"))
List(akari, kyoko, yui, chinatsu) sorted Order[Student].toScalaOrdering assert_=== List(akari, chinatsu, yui, kyoko)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment