Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Topic/kernel big decimal #1319

Merged
merged 4 commits into from
Aug 22, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ lazy val kernel = crossProject.crossType(CrossType.Pure)
.settings(scoverageSettings: _*)
.settings(sourceGenerators in Compile <+= (sourceManaged in Compile).map(KernelBoiler.gen))
.jsSettings(commonJsSettings:_*)
.jvmSettings((commonJvmSettings ++ (mimaPreviousArtifacts := Set("org.typelevel" %% "cats-kernel" % "0.6.0"))):_*)
.jvmSettings((commonJvmSettings ++ (mimaPreviousArtifacts := Set("org.typelevel" %% "cats-kernel" % "0.7.0"))):_*)

lazy val kernelJVM = kernel.jvm
lazy val kernelJS = kernel.js
Expand Down
40 changes: 39 additions & 1 deletion kernel-laws/src/main/scala/cats/kernel/laws/OrderLaws.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import org.typelevel.discipline.Laws
import org.scalacheck.{Arbitrary, Prop}
import org.scalacheck.Prop._

import cats.kernel.instances.boolean._
import cats.kernel.instances.all._

object OrderLaws {
def apply[A: Eq: Arbitrary]: OrderLaws[A] = new OrderLaws[A] {
Expand Down Expand Up @@ -59,6 +59,26 @@ trait OrderLaws[A] extends Laws {
},
"gt" -> forAll { (x: A, y: A) =>
A.lt(x, y) ?== A.gt(y, x)
},
"partialCompare" -> forAll { (x: A, y: A) =>
val c = A.partialCompare(x, y)
((c < 0) ?== A.lt(x, y)) && ((c == 0) ?== A.eqv(x, y)) && ((c > 0) ?== A.gt(x, y))
},
"pmin" -> forAll { (x: A, y: A) =>
val c = A.partialCompare(x, y)
val m = A.pmin(x, y)
if (c < 0) m ?== Some(x)
else if (c == 0) (m ?== Some(x)) && (m ?== Some(y))
else if (c > 0) m ?== Some(y)
else m ?== None
},
"pmax" -> forAll { (x: A, y: A) =>
val c = A.partialCompare(x, y)
val m = A.pmax(x, y)
if (c < 0) m ?== Some(y)
else if (c == 0) (m ?== Some(x)) && (m ?== Some(y))
else if (c > 0) m ?== Some(x)
else m ?== None
}
)

Expand All @@ -67,6 +87,24 @@ trait OrderLaws[A] extends Laws {
parent = Some(partialOrder),
"totality" -> forAll { (x: A, y: A) =>
A.lteqv(x, y) ?|| A.lteqv(y, x)
},
"compare" -> forAll { (x: A, y: A) =>
val c = A.compare(x, y)
((c < 0) ?== A.lt(x, y)) && ((c == 0) ?== A.eqv(x, y)) && ((c > 0) ?== A.gt(x, y))
},
"min" -> forAll { (x: A, y: A) =>
val c = A.compare(x, y)
val m = A.min(x, y)
if (c < 0) m ?== x
else if (c == 0) (m ?== x) && (m ?== y)
else m ?== y
},
"max" -> forAll { (x: A, y: A) =>
val c = A.compare(x, y)
val m = A.max(x, y)
if (c < 0) m ?== y
else if (c == 0) (m ?== x) && (m ?== y)
else m ?== x
}
)

Expand Down
8 changes: 8 additions & 0 deletions kernel-laws/src/test/scala/cats/kernel/laws/LawTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,14 @@ class LawTests extends FunSuite with Discipline {
//laws[GroupLaws, Double].check(_.commutativeGroup) // approximately associative
laws[GroupLaws, BigInt].check(_.commutativeGroup)

{
// default Arbitrary[BigDecimal] is a bit too intense :/
implicit val arbBigDecimal: Arbitrary[BigDecimal] =
Arbitrary(arbitrary[Double].map(n => BigDecimal(n.toString)))
laws[OrderLaws, BigDecimal].check(_.order)
laws[GroupLaws, BigDecimal].check(_.commutativeGroup)
}

laws[GroupLaws, (Int, Int)].check(_.band)

laws[GroupLaws, Unit].check(_.boundedSemilattice)
Expand Down
3 changes: 2 additions & 1 deletion kernel/src/main/scala/cats/kernel/instances/all.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ package instances
package object all extends AllInstances

trait AllInstances
extends BigIntInstances
extends BigDecimalInstances
with BigIntInstances
with BooleanInstances
with ByteInstances
with CharInstances
Expand Down
33 changes: 33 additions & 0 deletions kernel/src/main/scala/cats/kernel/instances/bigDecimal.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package cats.kernel
package instances

package object bigDecimal extends BigDecimalInstances // scalastyle:ignore package.object.name

trait BigDecimalInstances {
implicit val catsKernelStdOrderForBigDecimal: Order[BigDecimal] =
new BigDecimalOrder
implicit val catsKernelStdGroupForBigDecimal: CommutativeGroup[BigDecimal] =
new BigDecimalGroup
}

class BigDecimalGroup extends CommutativeGroup[BigDecimal] {
val empty: BigDecimal = BigDecimal(0)
def combine(x: BigDecimal, y: BigDecimal): BigDecimal = x + y
def inverse(x: BigDecimal): BigDecimal = -x
override def remove(x: BigDecimal, y: BigDecimal): BigDecimal = x - y
}

class BigDecimalOrder extends Order[BigDecimal] {

def compare(x: BigDecimal, y: BigDecimal): Int = x compare y

override def eqv(x: BigDecimal, y: BigDecimal): Boolean = x == y
override def neqv(x: BigDecimal, y: BigDecimal): Boolean = x != y
override def gt(x: BigDecimal, y: BigDecimal): Boolean = x > y
override def gteqv(x: BigDecimal, y: BigDecimal): Boolean = x >= y
override def lt(x: BigDecimal, y: BigDecimal): Boolean = x < y
override def lteqv(x: BigDecimal, y: BigDecimal): Boolean = x <= y

override def min(x: BigDecimal, y: BigDecimal): BigDecimal = x min y
override def max(x: BigDecimal, y: BigDecimal): BigDecimal = x max y
}