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 &> and <& as syntax for Parallel #2038

Merged
merged 2 commits into from
Nov 25, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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