Skip to content

Commit

Permalink
Add Hash instance for NonEmptyMap. (#2989)
Browse files Browse the repository at this point in the history
  • Loading branch information
tanaka takaya authored and LukaJCB committed Aug 15, 2019
1 parent 3cf3366 commit 0de7503
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
15 changes: 10 additions & 5 deletions core/src/main/scala/cats/data/NonEmptyMapImpl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ sealed class NonEmptyMapOps[K, A](val value: NonEmptyMap[K, A]) {
def toNel: NonEmptyList[(K, A)] = NonEmptyList.fromListUnsafe(toSortedMap.toList)
}

sealed abstract private[data] class NonEmptyMapInstances {
sealed abstract private[data] class NonEmptyMapInstances extends NonEmptyMapInstances0 {

implicit def catsDataInstancesForNonEmptyMap[K: Order]
: SemigroupK[NonEmptyMap[K, *]] with NonEmptyTraverse[NonEmptyMap[K, *]] =
Expand Down Expand Up @@ -318,10 +318,8 @@ sealed abstract private[data] class NonEmptyMapInstances {
NonEmptyList(fa.head._2, fa.tail.toList.map(_._2))
}

implicit def catsDataEqForNonEmptyMap[K: Order, A: Eq]: Eq[NonEmptyMap[K, A]] =
new Eq[NonEmptyMap[K, A]] {
def eqv(x: NonEmptyMap[K, A], y: NonEmptyMap[K, A]): Boolean = x === y
}
implicit def catsDataHashForNonEmptyMap[K: Hash: Order, A: Hash]: Hash[NonEmptyMap[K, A]] =
Hash[SortedMap[K, A]].asInstanceOf[Hash[NonEmptyMap[K, A]]]

implicit def catsDataShowForNonEmptyMap[K: Show, A: Show]: Show[NonEmptyMap[K, A]] =
Show.show[NonEmptyMap[K, A]](_.show)
Expand All @@ -330,3 +328,10 @@ sealed abstract private[data] class NonEmptyMapInstances {
def combine(x: NonEmptyMap[K, A], y: NonEmptyMap[K, A]): NonEmptyMap[K, A] = x ++ y
}
}

sealed abstract private[data] class NonEmptyMapInstances0 {
implicit def catsDataEqForNonEmptyMap[K: Order, A: Eq]: Eq[NonEmptyMap[K, A]] =
new Eq[NonEmptyMap[K, A]] {
def eqv(x: NonEmptyMap[K, A], y: NonEmptyMap[K, A]): Boolean = x === y
}
}
1 change: 1 addition & 0 deletions tests/src/test/scala/cats/tests/NonEmptyMapSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class NonEmptyMapSuite extends CatsSuite {
)
checkAll("NonEmptyMap[String, Int]", BandTests[NonEmptyMap[String, Int]].band)
checkAll("NonEmptyMap[String, Int]", EqTests[NonEmptyMap[String, Int]].eqv)
checkAll("NonEmptyMap[String, Int]", HashTests[NonEmptyMap[String, Int]].hash)

test("Show is not empty and is formatted as expected") {
forAll { (nem: NonEmptyMap[String, Int]) =>
Expand Down

0 comments on commit 0de7503

Please sign in to comment.