Skip to content

Commit

Permalink
CommutativeMonoid instance for SortedMap (#2047)
Browse files Browse the repository at this point in the history
* Add an instance of CommutativeMonoid for SortedMap

* Add a test for serializable CommutativeMonoid

* Add mima exclusions
  • Loading branch information
A. Alonso Dominguez authored and LukaJCB committed Nov 26, 2017
1 parent 563f612 commit d621581
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
2 changes: 2 additions & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down
14 changes: 11 additions & 3 deletions core/src/main/scala/cats/instances/sortedMap.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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]] {
Expand Down Expand Up @@ -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`.
Expand Down Expand Up @@ -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)
Expand Down
9 changes: 6 additions & 3 deletions tests/src/test/scala/cats/tests/SortedMapSuite.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package cats
package tests

import cats.kernel.laws.discipline.{HashTests, MonoidTests}
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._

Expand All @@ -28,6 +29,8 @@ 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("Monoid[SortedMap[String, Int]]", SerializableTests.serializable(Monoid[SortedMap[String, Int]]))
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, String]]", SerializableTests.serializable(Monoid[SortedMap[String, String]]))
}

0 comments on commit d621581

Please sign in to comment.