Skip to content

Commit

Permalink
Merge pull request #4612 from isomarcte/non-empty-vector-order
Browse files Browse the repository at this point in the history
Add Hash and Order Instances for NonEmptyVector
  • Loading branch information
rossabaker authored Jun 10, 2024
2 parents 1a0d029 + 4fafcf1 commit 90db3cd
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
22 changes: 20 additions & 2 deletions core/src/main/scala/cats/data/NonEmptyVector.scala
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ final class NonEmptyVector[+A] private (val toVector: Vector[A])
}

@suppressUnusedImportWarningForScalaVersionSpecific
sealed abstract private[data] class NonEmptyVectorInstances {
sealed abstract private[data] class NonEmptyVectorInstances extends NonEmptyVectorInstances0 {

@deprecated(
"maintained for the sake of binary compatibility only - use catsDataInstancesForNonEmptyChainBinCompat1 instead",
Expand Down Expand Up @@ -564,7 +564,11 @@ sealed abstract private[data] class NonEmptyVectorInstances {
NonEmptyVector.fromVectorUnsafe(Align[Vector].alignWith(fa.toVector, fb.toVector)(f))
}

implicit def catsDataEqForNonEmptyVector[A: Eq]: Eq[NonEmptyVector[A]] = _ === _
implicit def catsDataOrderForNonEmptyVector[A: Order]: Order[NonEmptyVector[A]] =
new Order[NonEmptyVector[A]] {
override def compare(x: NonEmptyVector[A], y: NonEmptyVector[A]): Int =
Order[Vector[A]].compare(x.toVector, y.toVector)
}

implicit def catsDataShowForNonEmptyVector[A: Show]: Show[NonEmptyVector[A]] = _.show

Expand Down Expand Up @@ -637,3 +641,17 @@ object NonEmptyVector extends NonEmptyVectorInstances with Serializable {
implicit def catsDataEqForZipNonEmptyVector[A: Eq]: Eq[ZipNonEmptyVector[A]] = Eq.by(_.value)
}
}

sealed abstract private[data] class NonEmptyVectorInstances0 extends NonEmptyVectorInstances1 {
implicit def catsDataPartialOrderForNonEmptyVector[A: PartialOrder]: PartialOrder[NonEmptyVector[A]] =
PartialOrder.by[NonEmptyVector[A], Vector[A]](_.toVector)
}

sealed abstract private[data] class NonEmptyVectorInstances1 extends NonEmptyVectorInstances2 {
implicit def catsDataHashForNonEmptyVector[A: Hash]: Hash[NonEmptyVector[A]] =
Hash.by(_.toVector)
}

sealed abstract private[data] class NonEmptyVectorInstances2 {
implicit def catsDataEqForNonEmptyVector[A: Eq]: Eq[NonEmptyVector[A]] = _ === _
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ import cats.data.NonEmptyVector
import cats.data.NonEmptyVector.ZipNonEmptyVector
import cats.kernel.instances.order.catsKernelOrderingForOrder
import cats.kernel.laws.discipline.EqTests
import cats.kernel.laws.discipline.HashTests
import cats.kernel.laws.discipline.OrderTests
import cats.kernel.laws.discipline.SemigroupTests
import cats.kernel.laws.discipline.PartialOrderTests
import cats.laws.discipline._
import cats.laws.discipline.arbitrary._
import cats.platform.Platform
Expand All @@ -49,6 +52,9 @@ class NonEmptyVectorSuite extends NonEmptyCollectionSuite[Vector, NonEmptyVector
Parameters.default.withMinSuccessfulTests(20).withMaxSize(Parameters.default.minSize + 5)

checkAll("NonEmptyVector[Int]", EqTests[NonEmptyVector[Int]].eqv)
checkAll("NonEmptyVector[Int]", HashTests[NonEmptyVector[Int]].hash)
checkAll("NonEmptyVector[Int]", OrderTests[NonEmptyVector[Int]].order)
checkAll("NonEmptyVector[Int]", PartialOrderTests[NonEmptyVector[Int]].partialOrder)

checkAll("NonEmptyVector[Int] with Option",
NonEmptyTraverseTests[NonEmptyVector].nonEmptyTraverse[Option, Int, Int, Int, Int, Option, Option]
Expand Down

0 comments on commit 90db3cd

Please sign in to comment.