Skip to content

Commit

Permalink
More examples
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonathan Coveney committed Sep 17, 2014
1 parent 14834a5 commit 3b408e9
Showing 1 changed file with 22 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,31 @@ class TypeclassBijectionLaws extends PropSpec with PropertyChecks with MustMatch
import TypeclassBijection._

case class A(x: Int, y: String)
implicit val bij: Bijection[A, (Int, String)] = Bijection.build[A, (Int, String)] { A.unapply(_).get } { x => A(x._1, x._2) }
implicit val abij: Bijection[A, (Int, String)] = Bijection.build[A, (Int, String)] { A.unapply(_).get } { x => A(x._1, x._2) }

implicit val orderingTypeclassBijection: TypeclassBijection[Ordering] = new TypeclassBijection[Ordering] {
def apply[A, B](tc: Ordering[A], bij: Bijection[A, B]) = tc.on { bij.invert(_) }
}

implicit val numericTypeclassBijection: TypeclassBijection[Numeric] = new TypeclassBijection[Numeric] {
def apply[A, B](tc: Numeric[A], bij: Bijection[A, B]) =
new Numeric[B] {
def plus(x: B, y: B) = bij(tc.plus(bij.invert(x), bij.invert(y)))
def minus(x: B, y: B) = bij(tc.minus(bij.invert(x), bij.invert(y)))
def times(x: B, y: B) = bij(tc.times(bij.invert(x), bij.invert(y)))
def negate(x: B) = bij(tc.negate(bij.invert(x)))
def fromInt(x: Int) = bij(tc.fromInt(x))
def toInt(x: B) = tc.toInt(bij.invert(x))
def toLong(x: B) = tc.toLong(bij.invert(x))
def toFloat(x: B) = tc.toFloat(bij.invert(x))
def toDouble(x: B) = tc.toDouble(bij.invert(x))
def compare(x: B, y: B) = tc.compare(bij.invert(x), bij.invert(y))
}
}

case class Wrapper(get: Int)
implicit val wrapperbij: Bijection[Wrapper, Int] = Bijection.build[Wrapper, Int] { _.get } { Wrapper(_) }

property("basic") {
implicitly[Semigroup[Int]]
implicitly[Semigroup[String]]
Expand All @@ -22,6 +41,8 @@ class TypeclassBijectionLaws extends PropSpec with PropertyChecks with MustMatch
implicitly[Semigroup[(Int, String)]].bijectTo[A]
deriveFor[Semigroup, A]
deriveFor[Ordering, A]
deriveFor[Ordering, Wrapper]
deriveFor[Numeric, Wrapper]
}
}

Expand Down

0 comments on commit 3b408e9

Please sign in to comment.