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 #2716

Closed
johnynek opened this issue Jan 31, 2019 · 2 comments
Closed

add method on object CommutativeApplicative to get CommutativeMonoid #2716

johnynek opened this issue Jan 31, 2019 · 2 comments

Comments

@johnynek
Copy link
Contributor

object CommutativeApplicative {
  def commutativeMonoidFor[F[_]: CommutativeApplicative, A: CommutativeMonoid]: CommutativeMonoid[F[A]]
}

object CommutativeApply {
  def commutativeSemigroupFor[F[_]: CommutativeApply, A: CommutativeSemigroup]: CommutativeSemigroup[F[A]]
}

These show how UnorderedFoldable and UnorderedReducible can have a def unorderedTraverse_ and def unorderedSequence_.

Which can be useful on things like hash-sets and hash-maps (and heaps).

@alexeygorobets
Copy link
Contributor

So it is like

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)
      }
    }
}

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)
      }
    }
}

or I am missing something?

@johnynek
Copy link
Contributor Author

johnynek commented Mar 9, 2019

I think that’s it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants