Skip to content

Commit

Permalink
Use helper functions in generated code
Browse files Browse the repository at this point in the history
  • Loading branch information
joroKr21 committed Sep 17, 2022
1 parent 2e0d307 commit 9ae2ce5
Showing 1 changed file with 36 additions and 51 deletions.
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 @@ -352,12 +350,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 @@ -372,7 +368,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 @@ -434,7 +430,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 @@ -449,14 +445,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 @@ -466,8 +457,7 @@ object Boilerplate {

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 @@ -476,10 +466,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
-}
Expand All @@ -494,20 +484,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 @@ -527,11 +507,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 @@ -545,22 +524,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 @@ -605,9 +587,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

0 comments on commit 9ae2ce5

Please sign in to comment.