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 all 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
3 changes: 2 additions & 1 deletion core/src/main/scala/cats/syntax/all.scala
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,6 @@ trait AllSyntaxBinCompat0
with TrySyntax

trait AllSyntaxBinCompat1
extends FlatMapOptionSyntax
extends FlatMapOptionSyntax
with NestedSyntax
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
9 changes: 9 additions & 0 deletions tests/src/test/scala/cats/tests/SyntaxSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,15 @@ object SyntaxSuite extends AllInstances with AllSyntax with AllSyntaxBinCompat1
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