Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add method on object CommutativeApplicative to get CommutativeMonoid #2747

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions core/src/main/scala/cats/CommutativeApplicative.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package cats

import cats.kernel.CommutativeMonoid
import simulacrum.typeclass

/**
Expand All @@ -12,3 +13,16 @@ import simulacrum.typeclass
* Must obey the laws defined in cats.laws.CommutativeApplicativeLaws.
*/
@typeclass trait CommutativeApplicative[F[_]] extends Applicative[F] with CommutativeApply[F]

object CommutativeApplicative {
def commutativeMonoidFor[F[_]: CommutativeApplicative, A: CommutativeMonoid]: CommutativeMonoid[F[A]] =
new CommutativeMonoid[F[A]] {
override def empty: F[A] =
CommutativeApplicative[F]
.pure(CommutativeMonoid[A].empty)

override def combine(x: F[A], y: F[A]): F[A] =
CommutativeApplicative[F]
.map2(x, y)(CommutativeMonoid[A].combine)
}
}
10 changes: 10 additions & 0 deletions core/src/main/scala/cats/CommutativeApply.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package cats

import cats.kernel.CommutativeSemigroup
import simulacrum.typeclass

/**
Expand All @@ -12,3 +13,12 @@ import simulacrum.typeclass
* Must obey the laws defined in cats.laws.CommutativeApplyLaws.
*/
@typeclass trait CommutativeApply[F[_]] extends Apply[F]

object CommutativeApply {
def commutativeSemigroupFor[F[_]: CommutativeApply, A: CommutativeSemigroup]: CommutativeSemigroup[F[A]] =
new CommutativeSemigroup[F[A]] {
override def combine(x: F[A], y: F[A]): F[A] =
CommutativeApply[F]
.map2(x, y)(CommutativeSemigroup[A].combine)
}
}
10 changes: 10 additions & 0 deletions tests/src/test/scala/cats/tests/AlgebraInvariantSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,16 @@ class AlgebraInvariantSuite extends CatsSuite {
checkAll("CommutativeSemigroup[Int]", CommutativeSemigroupTests[Int](S).commutativeSemigroup)
}

{
val S: CommutativeSemigroup[Option[Int]] = CommutativeApply.commutativeSemigroupFor[Option, Int]
checkAll("CommutativeSemigroup[Option[Int]", CommutativeSemigroupTests[Option[Int]](S).commutativeSemigroup)
}

{
val S: CommutativeMonoid[Option[Int]] = CommutativeApplicative.commutativeMonoidFor[Option, Int]
checkAll("CommutativeMonoid[Option[Int]", CommutativeMonoidTests[Option[Int]](S).commutativeMonoid)
}

{
val S: CommutativeMonoid[Int] = CommutativeMonoid[Int].imap(identity)(identity)
checkAll("CommutativeMonoid[Int]", CommutativeMonoidTests[Int](S).commutativeMonoid)
Expand Down