From 007ea26ce8c99ec206b338e5447d38f5fe009c16 Mon Sep 17 00:00:00 2001 From: Georgi Krastev Date: Sat, 17 Sep 2022 23:26:37 +0200 Subject: [PATCH] Use helper functions in generated code --- project/Boilerplate.scala | 87 ++++++++++++++++----------------------- 1 file changed, 36 insertions(+), 51 deletions(-) diff --git a/project/Boilerplate.scala b/project/Boilerplate.scala index 996e385dab..5fb059c758 100644 --- a/project/Boilerplate.scala +++ b/project/Boilerplate.scala @@ -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(", ") @@ -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 @@ -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)) |} """ } @@ -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 @@ -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) |} """ } @@ -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`}) } @@ -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) @@ -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 @@ -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 @@ -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) @@ -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) @@ -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 -} | """ @@ -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) |} """ }