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 .parFlatTraverse and .parFlatSequence syntax #2256

Merged
merged 5 commits into from
May 23, 2018
Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 1 addition & 1 deletion core/src/main/scala/cats/syntax/all.scala
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,4 @@ trait AllSyntaxBinCompat0
with ApplicativeErrorExtension
with TrySyntax

trait AllSyntaxBinCompat1 extends FlatMapOptionSyntax
trait AllSyntaxBinCompat1 extends FlatMapOptionSyntax with ParallelFlatSyntax
17 changes: 17 additions & 0 deletions core/src/main/scala/cats/syntax/parallel.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,37 @@ trait ParallelSyntax extends TupleParallelSyntax {
new ParallelApOps[M, A](ma)
}

trait ParallelFlatSyntax {
implicit final def catsSyntaxParallelFlatTraverse[T[_]: Traverse: FlatMap, A]
(ta: T[A]): ParallelFlatTraversableOps[T, A] = new ParallelFlatTraversableOps[T, A](ta)

implicit final def catsSyntaxParallelFlatSequence[T[_]: Traverse: FlatMap, M[_]: Monad, A]
(tmta: T[M[T[A]]]): ParallelFlatSequenceOps[T, M, A] = new ParallelFlatSequenceOps[T, M, A](tmta)
}

final class ParallelTraversableOps[T[_], A](val ta: T[A]) extends AnyVal {

def parTraverse[M[_]: Monad, F[_], B]
(f: A => M[B])(implicit T: Traverse[T], P: Parallel[M, F]): M[T[B]] =
Parallel.parTraverse(ta)(f)
}

final class ParallelFlatTraversableOps[T[_], A](val ta: T[A]) extends AnyVal {
def parFlatTraverse[M[_]: Monad, F[_], B]
(f: A => M[T[B]])(implicit T0: Traverse[T], T1 : FlatMap[T], P: Parallel[M, F]): M[T[B]] =
Parallel.parFlatTraverse(ta)(f)
}

final class ParallelSequenceOps[T[_], M[_], A](val tma: T[M[A]]) extends AnyVal {
def parSequence[F[_]]
(implicit M: Monad[M], T: Traverse[T], P: Parallel[M, F]): M[T[A]] =
Parallel.parSequence(tma)
}

final class ParallelFlatSequenceOps[T[_], M[_], A](val tmta: T[M[T[A]]]) extends AnyVal {
def parFlatSequence[F[_]]
(implicit M: Monad[M], T0: Traverse[T], T1 : FlatMap[T], P: Parallel[M, F]): M[T[A]] =
Parallel.parFlatSequence(tmta)
}

final class ParallelApOps[M[_], A](val ma: M[A]) extends AnyVal {
Expand Down
14 changes: 14 additions & 0 deletions tests/src/test/scala/cats/tests/ParallelSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,20 @@ class ParallelSuite extends CatsSuite with ApplicativeErrorForEitherTest {
}
}

test("ParFlatSequence syntax should be equivalent to Parallel.parFlatSequence") {
forAll { es: List[Either[String, List[Int]]] =>
es.parFlatSequence should === (Parallel.parFlatSequence(es))
}
}

test("ParFlatTraverse syntax should be equivalent to Parallel.parFlatTraverse") {
forAll { es: List[Either[String, Int]] =>
val f: Int => List[Int] = i => List(i, i + 1)
Parallel.parFlatTraverse(es)(e => e.map(f)) should
=== (es.parFlatTraverse(e => e.map(f)))
}
}

test("ParNonEmptyFlatTraverse should be equivalent to parNonEmptyTraverse map flatten") {
forAll { es: NonEmptyList[Either[String, Int]] =>
val f: Int => NonEmptyList[Int] = i => NonEmptyList.of(i, i + 1)
Expand Down
13 changes: 11 additions & 2 deletions tests/src/test/scala/cats/tests/SyntaxSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package tests

import cats.arrow.Compose
import cats.instances.AllInstances
import cats.syntax.AllSyntax
import cats.syntax.{AllSyntax, AllSyntaxBinCompat1}


/**
Expand All @@ -24,7 +24,7 @@ import cats.syntax.AllSyntax
*
* None of these tests should ever run, or do any runtime checks.
*/
object SyntaxSuite extends AllInstances with AllSyntax {
object SyntaxSuite extends AllInstances with AllSyntax with AllSyntaxBinCompat1 {

// pretend we have a value of type A
def mock[A]: A = ???
Expand Down Expand Up @@ -179,6 +179,15 @@ object SyntaxSuite extends AllInstances with AllSyntax {
val ma2: M[A] = ma <& mb
}

def testParallelFlat[M[_]: Monad, F[_], T[_]: Traverse: FlatMap, A, B](implicit P: Parallel[M, F]): Unit = {
val ta = mock[T[A]]
val f = mock[A => M[T[B]]]
val mtb = ta.parFlatTraverse(f)

val tmta = mock[T[M[T[A]]]]
val mta = tmta.parFlatSequence
}

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