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 F[TupleN] syntax #4125

Closed
wants to merge 3 commits into from
Closed
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
2 changes: 1 addition & 1 deletion core/src/main/scala/cats/syntax/functor.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package cats
package syntax

trait FunctorSyntax extends Functor.ToFunctorOps {
trait FunctorSyntax extends Functor.ToFunctorOps with FunctorTupleNSyntax {
implicit final def catsSyntaxFunctorTuple2Ops[F[_], A, B](fab: F[(A, B)]): FunctorTuple2Ops[F, A, B] =
new FunctorTuple2Ops[F, A, B](fab)
}
Expand Down
3 changes: 2 additions & 1 deletion project/Boilerplate.scala
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ object Boilerplate {
GenTupleMonadInstances,
GenTupleBifunctorInstances,
GenTupleBitraverseInstances,
GenTupleUnorderedFoldableInstances
GenTupleUnorderedFoldableInstances,
GenFunctorTupleNSyntax
)

val header = "// auto-generated boilerplate by /project/Boilerplate.scala" // TODO: put something meaningful here?
Expand Down
43 changes: 43 additions & 0 deletions project/GenFunctorTupleNSyntax.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import sbt._

import Boilerplate._
import Boilerplate.{Template, TemplateVals}
import sbt.File

object GenFunctorTupleNSyntax extends Template {
// we generate syntax for Tuple3..22 because already there is [[cats.syntax.FunctorTuple2Ops]].
override def range = 3 to maxArity
override def filename(root: sbt.File): File =
root / "cats" / "syntax" / "FunctorTupleNSyntax.scala"

override def content(tv: TemplateVals): String = {
import tv._

val generatedFunctions: String =
(1 to arity)
.map { n =>
s"""
- /**
- * Lifts [[Tuple$arity._$n]] into `F[_]`.
- */
- def _${n}F(implicit F: Functor[F]): F[A${n - 1}] = F.map(ftuple)(_._$n)
-
"""
}
.mkString("\n")

block"""
|
|package cats
|package syntax
|
|trait FunctorTupleNSyntax {
- implicit final def catsSyntaxFunctorTuple${arity}Ops[F[_], ${`A..N`}](ftuple: F[(${`A..N`})]): FunctorTuple${arity}Ops[F, ${`A..N`}] = new FunctorTuple${arity}Ops[F, ${`A..N`}](ftuple)
-
- private[syntax] final class FunctorTuple${arity}Ops[F[_], ${`A..N`}](ftuple: F[(${`A..N`})]) extends Serializable {
$generatedFunctions
- }
-
|}"""
}
}
Loading