Skip to content

Commit

Permalink
Add syntax for separate from MonadCombine.
Browse files Browse the repository at this point in the history
I based myself on the structure in syntax.bitraverse for the two implicit conversions (the
existing for unite and the new for separate).

MonadCombine separate was added in typelevel#864 by @adelbertc.
  • Loading branch information
peterneyens committed Apr 11, 2016
1 parent 26efefa commit 60ab295
Showing 1 changed file with 28 additions and 5 deletions.
33 changes: 28 additions & 5 deletions core/src/main/scala/cats/syntax/monadCombine.scala
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
package cats
package syntax

trait MonadCombineSyntax {
trait MonadCombineSyntax extends MonadCombineSyntax1 {
// TODO: use simulacrum instances eventually
implicit def nestedMonadCombineSyntax[F[_]: MonadCombine, G[_], A](fga: F[G[A]]): NestedMonadCombineOps[F, G, A] =
new NestedMonadCombineOps[F, G, A](fga)
implicit def monadCombineSyntax[F[_]: MonadCombine, G[_], A](fga: F[G[A]]): MonadCombineOps[F, G, A] =
new MonadCombineOps[F, G, A](fga)
}

final class NestedMonadCombineOps[F[_], G[_], A](fga: F[G[A]])(implicit F: MonadCombine[F]) {
private[syntax] trait MonadCombineSyntax1 {
implicit def nestedMonadCombineSyntax[F[_]: MonadCombine, G[_, _], A, B](fgab: F[G[A, B]]): NestedMonadCombineOps[F, G, A, B] =
new NestedMonadCombineOps[F, G, A, B](fgab)
}

final class MonadCombineOps[F[_], G[_], A](fga: F[G[A]])(implicit F: MonadCombine[F]) {

/**
* @see [[MonadCombine.unite]]
*
* Example:
* {{{
* scala> import cats.std.list._
* scala> import cats.std.vector._
* scala> import cats.std.vector._
* scala> import cats.syntax.monadCombine._
* scala> val x: List[Vector[Int]] = List(Vector(1, 2), Vector(3, 4))
* scala> x.unite
Expand All @@ -24,3 +29,21 @@ final class NestedMonadCombineOps[F[_], G[_], A](fga: F[G[A]])(implicit F: Monad
*/
def unite(implicit G: Foldable[G]): F[A] = F.unite(fga)
}

final class NestedMonadCombineOps[F[_], G[_, _], A, B](fgab: F[G[A, B]])(implicit F: MonadCombine[F]) {

/**
* @see [[MonadCombine.separate]]
*
* Example:
* {{{
* scala> import cats.data.Xor
* scala> import cats.std.list._
* scala> import cats.syntax.monadCombine._
* scala> val l: List[Xor[String, Int]] = List(Xor.right(1), Xor.left("error"))
* scala> l.separate
* res0: (List[String], List[Int]) = (List(error),List(1))
* }}}
*/
def separate(implicit G: Bifoldable[G]): (F[A], F[B]) = F.separate(fgab)
}

0 comments on commit 60ab295

Please sign in to comment.