From 8ee9d855b73bca34edf89431eccc3e55844673f9 Mon Sep 17 00:00:00 2001 From: Antonio Alonso Dominguez Date: Sun, 26 Nov 2017 15:47:57 +0000 Subject: [PATCH 1/3] Add an instance of CommutativeMonoid for SortedMap --- core/src/main/scala/cats/instances/sortedMap.scala | 14 +++++++++++--- .../src/test/scala/cats/tests/SortedMapSuite.scala | 5 +++-- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/core/src/main/scala/cats/instances/sortedMap.scala b/core/src/main/scala/cats/instances/sortedMap.scala index 83d005bdd3..5a25a9c0ca 100644 --- a/core/src/main/scala/cats/instances/sortedMap.scala +++ b/core/src/main/scala/cats/instances/sortedMap.scala @@ -7,13 +7,13 @@ import cats.kernel.instances.StaticMethods import scala.annotation.tailrec import scala.collection.immutable.SortedMap -trait SortedMapInstances extends SortedMapInstances1 { +trait SortedMapInstances extends SortedMapInstances2 { implicit def catsStdHashForSortedMap[K: Hash: Order, V: Hash]: Hash[SortedMap[K, V]] = new SortedMapHash[K, V] - implicit def catsStdMonoidForSortedMap[K: Order, V: Semigroup]: Monoid[SortedMap[K, V]] = - new SortedMapMonoid[K, V] + implicit def catsStdCommutativeMonoidForSortedMap[K: Order, V: CommutativeSemigroup]: CommutativeMonoid[SortedMap[K, V]] = + new SortedMapCommutativeMonoid[K, V] implicit def catsStdShowForSortedMap[A: Order, B](implicit showA: Show[A], showB: Show[B]): Show[SortedMap[A, B]] = new Show[SortedMap[A, B]] { @@ -109,6 +109,11 @@ trait SortedMapInstances1 { new SortedMapEq[K, V] } +trait SortedMapInstances2 extends SortedMapInstances1 { + implicit def catsStdMonoidForSortedMap[K: Order, V: Semigroup]: Monoid[SortedMap[K, V]] = + new SortedMapMonoid[K, V] +} + class SortedMapHash[K, V](implicit V: Hash[V], O: Order[K], K: Hash[K]) extends SortedMapEq[K, V]()(V, O) with Hash[SortedMap[K, V]] { // adapted from [[scala.util.hashing.MurmurHash3]], // but modified standard `Any#hashCode` to `ev.hash`. @@ -142,6 +147,9 @@ class SortedMapEq[K, V](implicit V: Eq[V], O: Order[K]) extends Eq[SortedMap[K, } } +class SortedMapCommutativeMonoid[K, V](implicit V: CommutativeSemigroup[V], O: Order[K]) + extends SortedMapMonoid[K, V] with CommutativeMonoid[SortedMap[K, V]] + class SortedMapMonoid[K, V](implicit V: Semigroup[V], O: Order[K]) extends Monoid[SortedMap[K, V]] { def empty: SortedMap[K, V] = SortedMap.empty(O.toOrdering) diff --git a/tests/src/test/scala/cats/tests/SortedMapSuite.scala b/tests/src/test/scala/cats/tests/SortedMapSuite.scala index 118fb7afe5..b2bc1b79b1 100644 --- a/tests/src/test/scala/cats/tests/SortedMapSuite.scala +++ b/tests/src/test/scala/cats/tests/SortedMapSuite.scala @@ -1,7 +1,7 @@ package cats package tests -import cats.kernel.laws.discipline.{HashTests, MonoidTests} +import cats.kernel.laws.discipline.{HashTests, CommutativeMonoidTests, MonoidTests} import cats.laws.discipline.{FlatMapTests, SemigroupalTests, SerializableTests, TraverseTests} import cats.laws.discipline.arbitrary._ @@ -28,6 +28,7 @@ class SortedMapSuite extends CatsSuite { } checkAll("Hash[SortedMap[Int, String]]" , HashTests[SortedMap[Int, String]].hash) - checkAll("Monoid[SortedMap[String, Int]]", MonoidTests[SortedMap[String, Int]].monoid) + checkAll("CommutativeMonoid[SortedMap[String, Int]]", CommutativeMonoidTests[SortedMap[String, Int]].commutativeMonoid) + checkAll("Monoid[SortedMap[String, String]]", MonoidTests[SortedMap[String, String]].monoid) checkAll("Monoid[SortedMap[String, Int]]", SerializableTests.serializable(Monoid[SortedMap[String, Int]])) } From 153c1dc2d1a3e00e9206cf878d2911dd4e4c9547 Mon Sep 17 00:00:00 2001 From: Antonio Alonso Dominguez Date: Sun, 26 Nov 2017 15:55:58 +0000 Subject: [PATCH 2/3] Add a test for serializable CommutativeMonoid --- tests/src/test/scala/cats/tests/SortedMapSuite.scala | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/src/test/scala/cats/tests/SortedMapSuite.scala b/tests/src/test/scala/cats/tests/SortedMapSuite.scala index b2bc1b79b1..33693c990b 100644 --- a/tests/src/test/scala/cats/tests/SortedMapSuite.scala +++ b/tests/src/test/scala/cats/tests/SortedMapSuite.scala @@ -1,6 +1,7 @@ package cats package tests +import cats.kernel.CommutativeMonoid import cats.kernel.laws.discipline.{HashTests, CommutativeMonoidTests, MonoidTests} import cats.laws.discipline.{FlatMapTests, SemigroupalTests, SerializableTests, TraverseTests} import cats.laws.discipline.arbitrary._ @@ -29,6 +30,7 @@ class SortedMapSuite extends CatsSuite { checkAll("Hash[SortedMap[Int, String]]" , HashTests[SortedMap[Int, String]].hash) checkAll("CommutativeMonoid[SortedMap[String, Int]]", CommutativeMonoidTests[SortedMap[String, Int]].commutativeMonoid) + checkAll("CommutativeMonoid[SortedMap[String, Int]]", SerializableTests.serializable(CommutativeMonoid[SortedMap[String, Int]])) checkAll("Monoid[SortedMap[String, String]]", MonoidTests[SortedMap[String, String]].monoid) - checkAll("Monoid[SortedMap[String, Int]]", SerializableTests.serializable(Monoid[SortedMap[String, Int]])) + checkAll("Monoid[SortedMap[String, String]]", SerializableTests.serializable(Monoid[SortedMap[String, String]])) } From e8025a625de669016d400017d489857dc791c71b Mon Sep 17 00:00:00 2001 From: Antonio Alonso Dominguez Date: Sun, 26 Nov 2017 21:02:57 +0000 Subject: [PATCH 3/3] Add mima exclusions --- build.sbt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/build.sbt b/build.sbt index c57abfeb75..35a0d65892 100644 --- a/build.sbt +++ b/build.sbt @@ -229,6 +229,8 @@ def mimaSettings(moduleName: String) = Seq( exclude[IncompatibleResultTypeProblem]("cats.instances.package#map.catsStdInstancesForMap"), exclude[IncompatibleResultTypeProblem]("cats.instances.package#set.catsStdInstancesForSet"), exclude[IncompatibleResultTypeProblem]("cats.instances.MapInstances.catsStdInstancesForMap"), + exclude[ReversedMissingMethodProblem]("cats.instances.SortedMapInstances.catsStdCommutativeMonoidForSortedMap"), + exclude[UpdateForwarderBodyProblem]("cats.instances.SortedMapInstances.catsStdMonoidForSortedMap"), exclude[DirectMissingMethodProblem]("cats.data.EitherTInstances2.catsDataMonadErrorForEitherT"), exclude[MissingTypesProblem]("cats.data.OneAndLowPriority3"), exclude[MissingTypesProblem]("cats.data.OneAndLowPriority2"),