Skip to content

Commit

Permalink
MonoidK instances for Map and SortedMap (#2744)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeygorobets authored and kailuowang committed Mar 7, 2019
1 parent e248dcc commit eab04cf
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 21 deletions.
1 change: 1 addition & 0 deletions core/src/main/scala/cats/implicits.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ object implicits
with instances.AllInstancesBinCompat1
with instances.AllInstancesBinCompat2
with instances.AllInstancesBinCompat3
with instances.AllInstancesBinCompat4
2 changes: 2 additions & 0 deletions core/src/main/scala/cats/instances/all.scala
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,5 @@ trait AllInstancesBinCompat1
trait AllInstancesBinCompat2 extends DurationInstances with FiniteDurationInstances

trait AllInstancesBinCompat3 extends AllCoreDurationInstances

trait AllInstancesBinCompat4 extends SortedMapInstancesBinCompat1 with MapInstancesBinCompat1
8 changes: 8 additions & 0 deletions core/src/main/scala/cats/instances/map.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
2 changes: 1 addition & 1 deletion core/src/main/scala/cats/instances/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 9 additions & 1 deletion core/src/main/scala/cats/instances/sortedMap.scala
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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
}
}
20 changes: 3 additions & 17 deletions testkit/src/main/scala/cats/tests/CatsSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down Expand Up @@ -55,6 +40,7 @@ trait CatsSuite
with AllInstancesBinCompat1
with AllInstancesBinCompat2
with AllInstancesBinCompat3
with AllInstancesBinCompat4
with AllSyntax
with AllSyntaxBinCompat0
with AllSyntaxBinCompat1
Expand Down
5 changes: 4 additions & 1 deletion tests/src/test/scala/cats/tests/MapSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import cats.laws.discipline.{
ComposeTests,
FlatMapTests,
FunctorFilterTests,
MonoidKTests,
SemigroupalTests,
SerializableTests,
UnorderedTraverseTests
Expand All @@ -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, ?]]))
Expand All @@ -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)
Expand Down
12 changes: 11 additions & 1 deletion tests/src/test/scala/cats/tests/SortedMapSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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, ?]]))
}

0 comments on commit eab04cf

Please sign in to comment.