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

Use helper functions in generated code #4303

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
87 changes: 36 additions & 51 deletions project/Boilerplate.scala
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,7 @@ object Boilerplate {
val synTypedVals = synVals.zip(synTypes).map { case (v, t) => v + ":" + t }
val `A..N` = synTypes.mkString(", ")
val `a..n` = synVals.mkString(", ")
val `_.._` = Seq.fill(arity)("_").mkString(", ")
val `(A..N)` = if (arity == 1) "Tuple1[A0]" else synTypes.mkString("(", ", ", ")")
val `(_.._)` = if (arity == 1) "Tuple1[_]" else Seq.fill(arity)("_").mkString("(", ", ", ")")
val `(a..n)` = if (arity == 1) "Tuple1(a)" else synVals.mkString("(", ", ", ")")
val `a:A..n:N` = synTypedVals.mkString(", ")

Expand Down Expand Up @@ -284,11 +282,10 @@ object Boilerplate {
def content(tv: TemplateVals) = {
import tv._

val tpes = synTypes.map { tpe =>
s"F[$tpe]"
}
val fargs = (0 until arity).map("f" + _)
val fparams = fargs.zip(tpes).map { case (v, t) => s"$v:$t" }.mkString(", ")
val vals = (0 until arity).map("f" + _)
val tpes = synTypes.map(tpe => s"F[$tpe]")
val fargs = vals.mkString(", ")
val fparams = vals.zip(tpes).map { case (v, t) => s"$v:$t" }.mkString(", ")

block"""
|package cats
Expand All @@ -302,7 +299,8 @@ object Boilerplate {
| */
|trait FlatMapArityFunctions[F[_]] { self: FlatMap[F] =>
- /** @group FlatMapArity */
- def flatMap$arity[${`A..N`}, Z]($fparams)(f: (${`A..N`}) => F[Z]): F[Z] = self.flatten(self.map$arity($fparams)(f))
- def flatMap$arity[${`A..N`}, Z]($fparams)(f: (${`A..N`}) => F[Z]): F[Z] =
- flatten(map$arity($fargs)(f))
|}
"""
}
Expand Down Expand Up @@ -355,12 +353,10 @@ object Boilerplate {
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 nestedExpansion = ParallelNestedExpansions(arity)
val vals = (0 until arity).map("m" + _)
val tpes = synTypes.map(tpe => s"M[$tpe]")
val fargs = vals.mkString(", ")
val fparams = vals.zip(tpes).map { case (v, t) => s"$v:$t" }.mkString(", ")

block"""
|package cats
Expand All @@ -375,7 +371,7 @@ object Boilerplate {
|abstract class ParallelArityFunctions2 extends ParallelArityFunctions {
- /** @group ParTupleArity */
- def parTuple$arity[M[_], ${`A..N`}]($fparams)(implicit p: NonEmptyParallel[M]): M[(${`A..N`})] =
- p.flatMap.map(${nestedExpansion.products}) { case ${nestedExpansion.`(a..n)`} => (${`a..n`}) }
- parMap$arity($fargs)(Tuple$arity.apply)
|}
"""
}
Expand Down Expand Up @@ -437,7 +433,7 @@ object Boilerplate {
- invariant.imap($nestedProducts) { case ${`nested (a..n)`} => f(${`a..n`}) } { z => val ${`(a..n)`} = g(z); ${`nested (a..n)`} }
- /** @group TupleArity */
- def tuple$arity[F[_], ${`A..N`}]($fparams)(implicit semigroupal: Semigroupal[F], invariant: Invariant[F]):F[(${`A..N`})] =
- imap$arity($fargsS)((${`_.._`}))(identity)
- imap$arity($fargsS)(Tuple$arity.apply)(identity)
- /** @group TraverseArity */
- def traverse$arity[F[_], G[_], ${`A..N`}, Z]($fparams)(f: (${`A..N`}) => G[Z])(implicit semigroupal: Semigroupal[F], traverse: Traverse[F], applicative: Applicative[G]): G[F[Z]] =
- traverse.traverse($nestedProducts) { case ${`nested (a..n)`} => f(${`a..n`}) }
Expand All @@ -452,14 +448,9 @@ object Boilerplate {
def content(tv: TemplateVals) = {
import tv._

val tpes = synTypes.map { tpe =>
s"M[$tpe]"
}
val tpesString = tpes.mkString(", ")

val tuple = s"Tuple$arity[$tpesString]"
val tupleTpe = s"t$arity: $tuple"
val tupleArgs = (1 to arity).map(n => s"t$arity._$n").mkString(", ")
val tpes = synTypes.map(tpe => s"M[$tpe]")
val tuple = if (arity == 1) s"Tuple1[${tpes.head}]" else tpes.mkString("(", ", ", ")")
val tupleArgs = (1 to arity).map(n => s"t._$n").mkString(", ")

val parMap =
if (arity == 1)
Expand All @@ -474,8 +465,7 @@ object Boilerplate {
s"def parFlatMapN[Z](f: (${`A..N`}) => M[Z])(implicit p: NonEmptyParallel[M]): M[Z] = Parallel.parFlatMap$arity($tupleArgs)(f)"
val parTupled =
if (arity == 1) ""
else
s"def parTupled(implicit p: NonEmptyParallel[M]): M[(${`A..N`})] = Parallel.parTuple$arity($tupleArgs)"
else s"def parTupled(implicit p: NonEmptyParallel[M]): M[(${`A..N`})] = Parallel.parTuple$arity($tupleArgs)"

block"""
|package cats
Expand All @@ -484,10 +474,10 @@ object Boilerplate {
|import cats.Parallel
|
|trait TupleParallelSyntax {
- implicit def catsSyntaxTuple${arity}Parallel[M[_], ${`A..N`}]($tupleTpe): Tuple${arity}ParallelOps[M, ${`A..N`}] = new Tuple${arity}ParallelOps(t$arity)
- implicit def catsSyntaxTuple${arity}Parallel[M[_], ${`A..N`}](t: $tuple): Tuple${arity}ParallelOps[M, ${`A..N`}] = new Tuple${arity}ParallelOps(t)
|}
|
-private[syntax] final class Tuple${arity}ParallelOps[M[_], ${`A..N`}](private val $tupleTpe) extends Serializable {
-private[syntax] final class Tuple${arity}ParallelOps[M[_], ${`A..N`}](private val t: $tuple) extends Serializable {
- $parMap
- $parTupled
- $parFlatMap
Expand All @@ -503,20 +493,10 @@ object Boilerplate {
def content(tv: TemplateVals) = {
import tv._

val tpes = synTypes.map { tpe =>
s"F[$tpe]"
}
val tpesString = tpes.mkString(", ")

val tuple = s"Tuple$arity[$tpesString]"
val tupleTpe = s"t$arity: $tuple"
val tupleArgs = (1 to arity).map(n => s"t$arity._$n").mkString(", ")

val n = if (arity == 1) {
""
} else {
arity.toString
}
val tpes = synTypes.map(tpe => s"F[$tpe]")
val tuple = if (arity == 1) s"Tuple1[${tpes.head}]" else tpes.mkString("(", ", ", ")")
val tupleArgs = (1 to arity).map(n => s"t._$n").mkString(", ")
val n = if (arity == 1) "" else arity.toString

val map =
if (arity == 1)
Expand All @@ -536,11 +516,10 @@ object Boilerplate {
else
s"def imapN[Z](f: (${`A..N`}) => Z)(g: Z => (${`A..N`}))(implicit invariant: Invariant[F], semigroupal: Semigroupal[F]): F[Z] = Semigroupal.imap$arity($tupleArgs)(f)(g)"

val tupled = if (arity != 1) {
s"def tupled(implicit invariant: Invariant[F], semigroupal: Semigroupal[F]): F[(${`A..N`})] = Semigroupal.tuple$n($tupleArgs)"
} else {
""
}
val tupled =
if (arity == 1) ""
else
s"def tupled(implicit invariant: Invariant[F], semigroupal: Semigroupal[F]): F[(${`A..N`})] = Semigroupal.tuple$n($tupleArgs)"

val traverse =
if (arity == 1)
Expand All @@ -554,22 +533,25 @@ object Boilerplate {
else
s"def flatMapN[Z](f: (${`A..N`}) => F[Z])(implicit flatMap: FlatMap[F]): F[Z] = flatMap.flatMap$arity($tupleArgs)(f)"

val apWith =
s"def apWith[Z](f: F[(${`A..N`}) => Z])(implicit apply: Apply[F]): F[Z] = apply.ap$n(f)($tupleArgs)"

block"""
|package cats
|package syntax
|
|trait TupleSemigroupalSyntax {
- implicit def catsSyntaxTuple${arity}Semigroupal[F[_], ${`A..N`}]($tupleTpe): Tuple${arity}SemigroupalOps[F, ${`A..N`}] = new Tuple${arity}SemigroupalOps(t$arity)
- implicit def catsSyntaxTuple${arity}Semigroupal[F[_], ${`A..N`}](t: $tuple): Tuple${arity}SemigroupalOps[F, ${`A..N`}] = new Tuple${arity}SemigroupalOps(t)
|}
|
-private[syntax] final class Tuple${arity}SemigroupalOps[F[_], ${`A..N`}](private val $tupleTpe) extends Serializable {
-private[syntax] final class Tuple${arity}SemigroupalOps[F[_], ${`A..N`}](private val t: $tuple) extends Serializable {
- $map
- $contramap
- $imap
- $flatMap
- $tupled
- $traverse
- def apWith[Z](f: F[(${`A..N`}) => Z])(implicit apply: Apply[F]): F[Z] = apply.ap$n(f)($tupleArgs)
- $apWith
-}
|
"""
Expand Down Expand Up @@ -614,9 +596,12 @@ object Boilerplate {
| *
| */
|trait FoldableNFunctions[F[_]] { self: Foldable[F] =>
| private def slidingN[A, B](fa: F[A], n: Int)(f: Seq[A] => B) =
| toIterable(fa).iterator.sliding(n).withPartial(false).map(f).toList
|
- /** @group FoldableSlidingN */
- def sliding$arity[A](fa: F[A]): List[$tupleTpe] =
- toIterable(fa).iterator.sliding($arity).withPartial(false).map(x => $tupleXN).toList
- slidingN(fa, $arity)(x => $tupleXN)
|}
"""
}
Expand Down