Created
January 26, 2015 16:53
-
-
Save julien-truffaut/2f15ca56be7ea8c1a934 to your computer and use it in GitHub Desktop.
type class without inheritance
This file contains hidden or 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
abstract class Equal[T]{ | |
def eq(v1: T, v2: T): Boolean | |
} | |
object Equal{ | |
def apply[T](implicit ev: Equal[T]): Equal[T] = ev | |
def equalA[T]: Equal[T] = new Equal[T] { | |
def eq(v1: T, v2: T): Boolean = v1 == v2 | |
} | |
implicit val intEqual = equalA[Int] | |
} | |
abstract class Ord[T: Equal]{ | |
def lowerThan(v1: T, v2: T): Boolean | |
def lowerThanOEqual(v1: T, v2: T): Boolean = lowerThan(v1, v2) || Equal[T].eq(v1, v2) | |
def greaterThan(v1: T, v2: T): Boolean = !lowerThan(v1, v2) | |
def greaterThanOEqual(v1: T, v2: T): Boolean = greaterThan(v1, v2) || Equal[T].eq(v1, v2) | |
} | |
object Ord{ | |
def apply[T](implicit ev: Ord[T]): Ord[T] = ev | |
def fromLowerThan[T: Equal](lth: (T, T) => Boolean): Ord[T] = new Ord[T] { | |
def lowerThan(v1: T, v2: T) = lth(v1, v2) | |
} | |
implicit val intOrd = fromLowerThan[Int](_ < _) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment