Skip to content

Commit

Permalink
Provide BoundedEnumerable[Byte] instance
Browse files Browse the repository at this point in the history
  • Loading branch information
ashleymercer committed Jan 31, 2021
1 parent ac3e754 commit a8d77cf
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -212,20 +212,19 @@ class Tests extends TestsConfig with DisciplineSuite {
checkAll("Order.reverse(Order.reverse(Order[Int]))", OrderTests(Order.reverse(Order.reverse(Order[Int]))).order)
checkAll("Order.fromLessThan[Int](_ < _)", OrderTests(Order.fromLessThan[Int](_ < _)).order)

checkAll("LowerBounded[Byte]", LowerBoundedTests[Byte].lowerBounded)
checkAll("LowerBounded[Duration]", LowerBoundedTests[Duration].lowerBounded)
checkAll("LowerBounded[FiniteDuration]", LowerBoundedTests[FiniteDuration].lowerBounded)
checkAll("LowerBounded[UUID]", LowerBoundedTests[UUID].lowerBounded)
checkAll("LowerBounded[String]", LowerBoundedTests[String].lowerBounded)
checkAll("LowerBounded[Symbol]", LowerBoundedTests[Symbol].lowerBounded)

checkAll("UpperBounded[Byte]", UpperBoundedTests[Byte].upperBounded)
checkAll("UpperBounded[Duration]", UpperBoundedTests[Duration].upperBounded)
checkAll("UpperBounded[FiniteDuration]", UpperBoundedTests[FiniteDuration].upperBounded)
checkAll("UpperBounded[UUID]", UpperBoundedTests[UUID].upperBounded)

checkAll("BoundedEnumerable[Unit]", BoundedEnumerableTests[Unit].boundedEnumerable)
checkAll("BoundedEnumerable[Boolean]", BoundedEnumerableTests[Boolean].boundedEnumerable)
checkAll("BoundedEnumerable[Byte]", BoundedEnumerableTests[Byte].boundedEnumerable)
checkAll("BoundedEnumerable[Short]", BoundedEnumerableTests[Short].boundedEnumerable)
checkAll("BoundedEnumerable[Int]", BoundedEnumerableTests[Int].boundedEnumerable)
checkAll("BoundedEnumerable[Char]", BoundedEnumerableTests[Char].boundedEnumerable)
Expand Down
2 changes: 2 additions & 0 deletions kernel/src/main/scala/cats/kernel/Enumerable.scala
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ object BoundedEnumerable {
cats.kernel.instances.unit.catsKernelStdOrderForUnit
implicit def catsKernelBoundedEnumerableForBoolean: BoundedEnumerable[Boolean] =
cats.kernel.instances.boolean.catsKernelStdOrderForBoolean
implicit def catsKernelBoundedEnumerableForByte: BoundedEnumerable[Byte] =
cats.kernel.instances.byte.catsKernelStdOrderForByte
implicit def catsKernelBoundedEnumerableForInt: BoundedEnumerable[Int] =
cats.kernel.instances.int.catsKernelStdOrderForInt
implicit def catsKernelBoundedEnumerableForShort: BoundedEnumerable[Short] =
Expand Down
13 changes: 10 additions & 3 deletions kernel/src/main/scala/cats/kernel/instances/ByteInstances.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package cats.kernel
package instances

trait ByteInstances {
implicit val catsKernelStdOrderForByte: Order[Byte] with Hash[Byte] with LowerBounded[Byte] with UpperBounded[Byte] =
implicit val catsKernelStdOrderForByte: Order[Byte] with Hash[Byte] with BoundedEnumerable[Byte] =
new ByteOrder
implicit val catsKernelStdGroupForByte: CommutativeGroup[Byte] = new ByteGroup
}
Expand All @@ -14,12 +14,19 @@ class ByteGroup extends CommutativeGroup[Byte] {
override def remove(x: Byte, y: Byte): Byte = (x - y).toByte
}

trait ByteEnumerable extends BoundedEnumerable[Byte] {
override def partialNext(a: Byte): Option[Byte] =
if (order.eqv(a, maxBound)) None else Some((a + 1).toByte)
override def partialPrevious(a: Byte): Option[Byte] =
if (order.eqv(a, minBound)) None else Some((a - 1).toByte)
}

trait ByteBounded extends LowerBounded[Byte] with UpperBounded[Byte] {
override def minBound: Byte = Byte.MinValue
override def maxBound: Byte = Byte.MaxValue
}

class ByteOrder extends Order[Byte] with Hash[Byte] with ByteBounded { self =>
class ByteOrder extends Order[Byte] with Hash[Byte] with ByteBounded with ByteEnumerable { self =>

def hash(x: Byte): Int = x.hashCode()

Expand All @@ -38,5 +45,5 @@ class ByteOrder extends Order[Byte] with Hash[Byte] with ByteBounded { self =>
override def max(x: Byte, y: Byte): Byte =
java.lang.Math.max(x.toInt, y.toInt).toByte

override val partialOrder: PartialOrder[Byte] = self
override val order: Order[Byte] = self
}

0 comments on commit a8d77cf

Please sign in to comment.