Skip to content

Commit

Permalink
Add &> and <& as syntax for Parallel
Browse files Browse the repository at this point in the history
  • Loading branch information
Luka Jacobowitz committed Nov 24, 2017
1 parent 9020258 commit cb32117
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
16 changes: 16 additions & 0 deletions core/src/main/scala/cats/Parallel.scala
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,22 @@ trait NonEmptyParallel[M[_], F[_]] extends Serializable {
*/
def parallel: M ~> F


/**
* Like `Apply[F].followedBy`, but uses the apply instance
* corresponding to the Parallel instance instead.
*/
def parFollowedBy[A, B](ma: M[A])(mb: M[B]): M[B] =
Parallel.parMap2(ma, mb)((_, b) => b)(this)


/**
* Like `Apply[F].forEffect`, but uses the apply instance
* corresponding to the Parallel instance instead.
*/
def parForEffect[A, B](ma: M[A])(mb: M[B]): M[A] =
Parallel.parMap2(ma, mb)((a, _) => a)(this)

}

/**
Expand Down
15 changes: 14 additions & 1 deletion core/src/main/scala/cats/syntax/parallel.scala
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package cats.syntax

import cats.{Monad, Parallel, Traverse}
import cats.{Monad, Parallel, Traverse, FlatMap}

trait ParallelSyntax extends TupleParallelSyntax {
implicit final def catsSyntaxParallelTraverse[T[_]: Traverse, A]
(ta: T[A]): ParallelTraversableOps[T, A] = new ParallelTraversableOps[T, A](ta)

implicit final def catsSyntaxParallelSequence[T[_]: Traverse, M[_]: Monad, A]
(tma: T[M[A]]): ParallelSequenceOps[T, M, A] = new ParallelSequenceOps[T, M, A](tma)

implicit final def catsSyntaxParallelAp[M[_]: FlatMap, A](ma: M[A]): ParallelApOps[M, A] =
new ParallelApOps[M, A](ma)
}


Expand All @@ -25,3 +28,13 @@ final class ParallelSequenceOps[T[_], M[_], A](val tma: T[M[A]]) extends AnyVal
Parallel.parSequence(tma)

}

final class ParallelApOps[M[_], A](val ma: M[A]) extends AnyVal {

def &>[F[_], B](mb: M[B])(implicit P: Parallel[M, F]): M[B] =
P.parFollowedBy(ma)(mb)

def <&[F[_], B](mb: M[B])(implicit P: Parallel[M, F]): M[A] =
P.parForEffect(ma)(mb)

}
6 changes: 6 additions & 0 deletions tests/src/test/scala/cats/tests/SyntaxSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,12 @@ object SyntaxSuite extends AllInstances with AllSyntax {

val tma = mock[T[M[A]]]
val mta = tma.parSequence

val ma = mock[M[A]]
val mb = mock[M[B]]

val mb2: M[B] = ma &> mb
val ma2: M[A] = ma <& mb
}

def testParallelTuple[M[_]: Monad, F[_], A, B, C, Z](implicit P: NonEmptyParallel[M, F]) = {
Expand Down

0 comments on commit cb32117

Please sign in to comment.