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 parTuple #2183

Merged
merged 8 commits into from
Mar 12, 2018
Merged
Show file tree
Hide file tree
Changes from 5 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/Parallel.scala
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ object NonEmptyParallel {
def apply[M[_], F[_]](implicit P: NonEmptyParallel[M, F]): NonEmptyParallel[M, F] = P
}

object Parallel extends ParallelArityFunctions {
object Parallel extends ParallelArityFunctions with ParallelArityFunctions2 {

def apply[M[_], F[_]](implicit P: Parallel[M, F]): Parallel[M, F] = P

Expand Down
47 changes: 43 additions & 4 deletions project/Boilerplate.scala
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ object Boilerplate {
GenApplyArityFunctions,
GenTupleSemigroupalSyntax,
GenParallelArityFunctions,
GenParallelArityFunctions2,
GenTupleParallelSyntax
)

Expand Down Expand Up @@ -213,6 +214,11 @@ object Boilerplate {
}
}

final case class ParallelNestedExpansions(arity: Int) {
val products = (0 until (arity - 2)).foldRight(s"Parallel.parProduct(m${arity - 2}, m${arity - 1})")((i, acc) => s"Parallel.parProduct(m$i, $acc)")
val `(a..n)` = (0 until (arity - 2)).foldRight(s"(a${arity - 2}, a${arity - 1})")((i, acc) => s"(a$i, $acc)")
}

object GenParallelArityFunctions extends Template {
def filename(root: File) = root / "cats" / "ParallelArityFunctions.scala"
override def range = 2 to maxArity
Expand All @@ -223,9 +229,7 @@ object Boilerplate {
val fargs = (0 until arity) map { "m" + _ }
val fparams = (fargs zip tpes) map { case (v,t) => s"$v:$t"} mkString ", "
val fargsS = fargs mkString ", "

val nestedProducts = (0 until (arity - 2)).foldRight(s"Parallel.parProduct(m${arity - 2}, m${arity - 1})")((i, acc) => s"Parallel.parProduct(m$i, $acc)")
val `nested (a..n)` = (0 until (arity - 2)).foldRight(s"(a${arity - 2}, a${arity - 1})")((i, acc) => s"(a$i, $acc)")
val nestedExpansion = ParallelNestedExpansions(arity)

block"""
|package cats
Expand All @@ -240,7 +244,38 @@ object Boilerplate {
|trait ParallelArityFunctions {
- /** @group ParMapArity */
- def parMap$arity[M[_], F[_], ${`A..N`}, Z]($fparams)(f: (${`A..N`}) => Z)(implicit p: NonEmptyParallel[M, F]): M[Z] =
- p.flatMap.map($nestedProducts) { case ${`nested (a..n)`} => f(${`a..n`}) }
- p.flatMap.map(${nestedExpansion.products}) { case ${nestedExpansion.`(a..n)`} => f(${`a..n`}) }
|}
"""
}
}

object GenParallelArityFunctions2 extends Template {
def filename(root: File) = root / "cats" / "ParallelArityFunctions2.scala"
override def range = 2 to maxArity
def content(tv: TemplateVals) = {
import tv._

val tpes = synTypes map { tpe => s"M[$tpe]" }
val fargs = (0 until arity) map { "m" + _ }
val fparams = (fargs zip tpes) map { case (v,t) => s"$v:$t"} mkString ", "
val fargsS = fargs mkString ", "
val nestedExpansion = ParallelNestedExpansions(arity)

block"""
|package cats
|
|/**
| * @groupprio Ungrouped 0
| *
| * @groupname ParTupleArity parTuple arity
| * @groupdesc ParTupleArity Higher-arity parTuple methods
| * @groupprio ParTupleArity 999
| */
|trait ParallelArityFunctions2 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we make this one an abstract class then we can add more methods in the future without breaking bin compat.

Copy link
Contributor Author

@barambani barambani Mar 6, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure, changed. To avoid problems mixing in the two with Parallel I extended ParallelArityFunctions with ParallelArityFunctions2. Do you think this will prevent from adding methods in the future ? Thanks

- /** @group ParTupleArity */
- def parTuple$arity[M[_], F[_], ${`A..N`}]($fparams)(implicit p: NonEmptyParallel[M, F]): M[(${`A..N`})] =
- p.flatMap.map(${nestedExpansion.products}) { case ${nestedExpansion.`(a..n)`} => (${`a..n`}) }
|}
"""
}
Expand Down Expand Up @@ -326,6 +361,9 @@ object Boilerplate {
if (arity == 1) s"def parMap[F[_], Z](f: (${`A..N`}) => Z)(implicit p: NonEmptyParallel[M, F]): M[Z] = p.flatMap.map($tupleArgs)(f)"
else s"def parMapN[F[_], Z](f: (${`A..N`}) => Z)(implicit p: NonEmptyParallel[M, F]): M[Z] = Parallel.parMap$arity($tupleArgs)(f)"

val parTupled =
if (arity == 1) ""
else s"def parTupled[F[_]](implicit p: NonEmptyParallel[M, F]): M[(${`A..N`})] = Parallel.parTuple$arity($tupleArgs)"

block"""
|package cats
Expand All @@ -339,6 +377,7 @@ object Boilerplate {
|
-private[syntax] final class Tuple${arity}ParallelOps[M[_], ${`A..N`}]($tupleTpe) {
- $parMap
- $parTupled
-}
|
"""
Expand Down