From b4ab2b9aa6b93b65883b236b6b0099631d8173a5 Mon Sep 17 00:00:00 2001 From: agorobets Date: Thu, 7 Mar 2019 09:47:09 +0300 Subject: [PATCH] MonoidK instances for Map and SortedMap --- core/src/main/scala/cats/implicits.scala | 1 + core/src/main/scala/cats/instances/all.scala | 2 ++ core/src/main/scala/cats/instances/map.scala | 8 ++++++++ .../main/scala/cats/instances/package.scala | 2 +- .../main/scala/cats/instances/sortedMap.scala | 10 +++++++++- .../src/main/scala/cats/tests/CatsSuite.scala | 20 +++---------------- .../src/test/scala/cats/tests/MapSuite.scala | 5 ++++- .../scala/cats/tests/SortedMapSuite.scala | 12 ++++++++++- 8 files changed, 39 insertions(+), 21 deletions(-) diff --git a/core/src/main/scala/cats/implicits.scala b/core/src/main/scala/cats/implicits.scala index 6094afa1b9..ae0155f8d7 100644 --- a/core/src/main/scala/cats/implicits.scala +++ b/core/src/main/scala/cats/implicits.scala @@ -12,3 +12,4 @@ object implicits with instances.AllInstancesBinCompat1 with instances.AllInstancesBinCompat2 with instances.AllInstancesBinCompat3 + with instances.AllInstancesBinCompat4 diff --git a/core/src/main/scala/cats/instances/all.scala b/core/src/main/scala/cats/instances/all.scala index b46054f9d2..29f19a9f1e 100644 --- a/core/src/main/scala/cats/instances/all.scala +++ b/core/src/main/scala/cats/instances/all.scala @@ -46,3 +46,5 @@ trait AllInstancesBinCompat1 trait AllInstancesBinCompat2 extends DurationInstances with FiniteDurationInstances trait AllInstancesBinCompat3 extends AllCoreDurationInstances + +trait AllInstancesBinCompat4 extends SortedMapInstancesBinCompat1 with MapInstancesBinCompat1 diff --git a/core/src/main/scala/cats/instances/map.scala b/core/src/main/scala/cats/instances/map.scala index 8ad14b2e98..e5669de7c6 100644 --- a/core/src/main/scala/cats/instances/map.scala +++ b/core/src/main/scala/cats/instances/map.scala @@ -134,3 +134,11 @@ trait MapInstancesBinCompat0 { } } + +trait MapInstancesBinCompat1 { + implicit def catsStdMonoidKForMap[K]: MonoidK[Map[K, ?]] = new MonoidK[Map[K, ?]] { + override def empty[A]: Map[K, A] = Map.empty + + override def combineK[A](x: Map[K, A], y: Map[K, A]): Map[K, A] = x ++ y + } +} diff --git a/core/src/main/scala/cats/instances/package.scala b/core/src/main/scala/cats/instances/package.scala index 60801f5e34..70eeec593f 100644 --- a/core/src/main/scala/cats/instances/package.scala +++ b/core/src/main/scala/cats/instances/package.scala @@ -22,7 +22,7 @@ package object instances { object list extends ListInstances with ListInstancesBinCompat0 object long extends LongInstances object option extends OptionInstances with OptionInstancesBinCompat0 - object map extends MapInstances with MapInstancesBinCompat0 + object map extends MapInstances with MapInstancesBinCompat0 with MapInstancesBinCompat1 object order extends OrderInstances object ordering extends OrderingInstances object parallel extends ParallelInstances diff --git a/core/src/main/scala/cats/instances/sortedMap.scala b/core/src/main/scala/cats/instances/sortedMap.scala index 37239fb8cc..54a5440b06 100644 --- a/core/src/main/scala/cats/instances/sortedMap.scala +++ b/core/src/main/scala/cats/instances/sortedMap.scala @@ -1,6 +1,6 @@ package cats.instances -import cats.{Always, Applicative, Eval, FlatMap, Foldable, Monoid, Order, Show, Traverse, TraverseFilter} +import cats.{Always, Applicative, Eval, FlatMap, Foldable, Monoid, MonoidK, Order, Show, Traverse, TraverseFilter} import cats.kernel._ import cats.kernel.instances.StaticMethods @@ -223,3 +223,11 @@ trait SortedMapInstancesBinCompat0 { traverseFilter(fa)(a => G.map(f(a))(if (_) Some(a) else None)) } } + +trait SortedMapInstancesBinCompat1 { + implicit def catsStdMonoidKForSortedMap[K: Order]: MonoidK[SortedMap[K, ?]] = new MonoidK[SortedMap[K, ?]] { + override def empty[A]: SortedMap[K, A] = SortedMap.empty[K, A](Order[K].toOrdering) + + override def combineK[A](x: SortedMap[K, A], y: SortedMap[K, A]): SortedMap[K, A] = x ++ y + } +} diff --git a/testkit/src/main/scala/cats/tests/CatsSuite.scala b/testkit/src/main/scala/cats/tests/CatsSuite.scala index b71890c035..7cfe0838dc 100644 --- a/testkit/src/main/scala/cats/tests/CatsSuite.scala +++ b/testkit/src/main/scala/cats/tests/CatsSuite.scala @@ -2,23 +2,8 @@ package cats package tests import catalysts.Platform - -import cats.instances.{ - AllInstances, - AllInstancesBinCompat0, - AllInstancesBinCompat1, - AllInstancesBinCompat2, - AllInstancesBinCompat3 -} -import cats.syntax.{ - AllSyntax, - AllSyntaxBinCompat0, - AllSyntaxBinCompat1, - AllSyntaxBinCompat2, - AllSyntaxBinCompat3, - AllSyntaxBinCompat4, - EqOps -} +import cats.instances._ +import cats.syntax._ import org.scalactic.anyvals.{PosInt, PosZDouble, PosZInt} import org.scalatest.{FunSuite, FunSuiteLike, Matchers} import org.scalatest.prop.{Configuration, GeneratorDrivenPropertyChecks} @@ -55,6 +40,7 @@ trait CatsSuite with AllInstancesBinCompat1 with AllInstancesBinCompat2 with AllInstancesBinCompat3 + with AllInstancesBinCompat4 with AllSyntax with AllSyntaxBinCompat0 with AllSyntaxBinCompat1 diff --git a/tests/src/test/scala/cats/tests/MapSuite.scala b/tests/src/test/scala/cats/tests/MapSuite.scala index efa600c567..ce92f78524 100644 --- a/tests/src/test/scala/cats/tests/MapSuite.scala +++ b/tests/src/test/scala/cats/tests/MapSuite.scala @@ -5,6 +5,7 @@ import cats.laws.discipline.{ ComposeTests, FlatMapTests, FunctorFilterTests, + MonoidKTests, SemigroupalTests, SerializableTests, UnorderedTraverseTests @@ -13,7 +14,6 @@ import cats.laws.discipline.arbitrary._ import cats.arrow.Compose class MapSuite extends CatsSuite { - implicit val iso = SemigroupalTests.Isomorphisms.invariant[Map[Int, ?]] checkAll("Map[Int, Int]", SemigroupalTests[Map[Int, ?]].semigroupal[Int, Int, Int]) checkAll("Semigroupal[Map[Int, ?]]", SerializableTests.serializable(Semigroupal[Map[Int, ?]])) @@ -31,6 +31,9 @@ class MapSuite extends CatsSuite { checkAll("Map[Int, Long]", ComposeTests[Map].compose[Int, Long, String, Double]) checkAll("Compose[Map]", SerializableTests.serializable(Compose[Map])) + checkAll("Map[Int, Int]", MonoidKTests[Map[Int, ?]].monoidK[Int]) + checkAll("MonoidK[Map[Int, ?]]", SerializableTests.serializable(MonoidK[Map[Int, ?]])) + test("show isn't empty and is formatted as expected") { forAll { (map: Map[Int, String]) => map.show.nonEmpty should ===(true) diff --git a/tests/src/test/scala/cats/tests/SortedMapSuite.scala b/tests/src/test/scala/cats/tests/SortedMapSuite.scala index d20adcb8ca..312d0c7116 100644 --- a/tests/src/test/scala/cats/tests/SortedMapSuite.scala +++ b/tests/src/test/scala/cats/tests/SortedMapSuite.scala @@ -3,7 +3,14 @@ package tests import cats.kernel.CommutativeMonoid import cats.kernel.laws.discipline.{CommutativeMonoidTests, HashTests, MonoidTests} -import cats.laws.discipline.{FlatMapTests, SemigroupalTests, SerializableTests, TraverseFilterTests, TraverseTests} +import cats.laws.discipline.{ + FlatMapTests, + MonoidKTests, + SemigroupalTests, + SerializableTests, + TraverseFilterTests, + TraverseTests +} import cats.laws.discipline.arbitrary._ import scala.collection.immutable.SortedMap @@ -39,4 +46,7 @@ class SortedMapSuite extends CatsSuite { SerializableTests.serializable(CommutativeMonoid[SortedMap[String, Int]])) checkAll("Monoid[SortedMap[String, String]]", MonoidTests[SortedMap[String, String]].monoid) checkAll("Monoid[SortedMap[String, String]]", SerializableTests.serializable(Monoid[SortedMap[String, String]])) + + checkAll("SortedMap[String, String]", MonoidKTests[SortedMap[String, ?]].monoidK[String]) + checkAll("MonoidK[SortedMap[String, ?]]", SerializableTests.serializable(MonoidK[SortedMap[String, ?]])) }