-
Notifications
You must be signed in to change notification settings - Fork 67
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
#313
Merged
Add F[TupleN]
syntax
#313
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import sbt._ | ||
|
||
import scala.annotation.tailrec | ||
import scala.collection.immutable | ||
|
||
/** | ||
* Copied, with some modifications, from https://github.com/milessabin/shapeless/blob/main/project/Boilerplate.scala | ||
* | ||
* Generate a range of boilerplate classes, those offering alternatives with 0-22 params and would be tedious to craft | ||
* by hand | ||
* | ||
* @author | ||
* Miles Sabin | ||
* @author | ||
* Kevin Wright | ||
*/ | ||
object Boilerplate { | ||
|
||
import scala.StringContext._ | ||
|
||
implicit final class BlockHelper(private val sc: StringContext) extends AnyVal { | ||
def block(args: Any*): String = { | ||
val interpolated = sc.standardInterpolator(treatEscapes, args) | ||
val rawLines = interpolated.split('\n') | ||
val trimmedLines = rawLines.map(_.dropWhile(_.isWhitespace)) | ||
trimmedLines.mkString("\n") | ||
} | ||
} | ||
|
||
val templates: Seq[Template] = Seq(GenFTupleNSyntax) | ||
|
||
val header: String = | ||
"""// auto-generated boilerplate by /project/Boilerplate.scala | ||
|package mouse | ||
|""".stripMargin | ||
|
||
/** | ||
* Returns a seq of the generated files. As a side-effect, it actually generates them... | ||
*/ | ||
def gen(dir: File) = | ||
for (t <- templates) yield { | ||
val tgtFile = t.filename(dir) | ||
IO.write(tgtFile, t.body) | ||
tgtFile | ||
} | ||
|
||
val maxArity: Int = 22 | ||
|
||
final class TemplateVals(val arity: Int) { | ||
val synTypes: immutable.Seq[String] = (0 until arity).map(n => s"A$n") | ||
val `A..N`: String = synTypes.mkString(", ") | ||
} | ||
|
||
trait Template { | ||
def filename(root: File): File | ||
|
||
def content(tv: TemplateVals): String | ||
|
||
def range = 1 to maxArity | ||
|
||
def body: String = { | ||
@tailrec | ||
def expandInstances(contents: IndexedSeq[Array[String]], acc: Array[String] = Array.empty): Array[String] = | ||
if (!contents.exists(_.exists(_.startsWith("-")))) | ||
acc.map(_.tail) | ||
else { | ||
val pre = contents.head.takeWhile(_.startsWith("|")) | ||
val instances = contents.flatMap(_.dropWhile(_.startsWith("|")).takeWhile(_.startsWith("-"))) | ||
val next = contents.map(_.dropWhile(_.startsWith("|")).dropWhile(_.startsWith("-"))) | ||
expandInstances(next, acc ++ pre ++ instances) | ||
} | ||
|
||
val rawContents = range.map { n => | ||
content(new TemplateVals(n)).split('\n').filterNot(_.isEmpty) | ||
} | ||
val headerLines = header.split('\n') | ||
val instances = expandInstances(rawContents) | ||
val footerLines = rawContents.head.reverse.takeWhile(_.startsWith("|")).map(_.tail).reverse | ||
(headerLines ++ instances ++ footerLines).mkString("\n") | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import sbt._ | ||
|
||
import Boilerplate._ | ||
import Boilerplate.{Template, TemplateVals} | ||
import sbt.File | ||
|
||
object GenFTupleNSyntax 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 / "mouse" / "FTupleNSyntax.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""" | ||
| | ||
|import cats.Functor | ||
| | ||
|trait FTupleNSyntax { | ||
- implicit final def mouseSyntaxFTuple${arity}Ops[F[_], ${`A..N`}](ftuple: F[(${`A..N`})]): FTuple${arity}Ops[F, ${`A..N`}] = new FTuple${arity}Ops[F, ${`A..N`}](ftuple) | ||
- | ||
- private[mouse] final class FTuple${arity}Ops[F[_], ${`A..N`}](ftuple: F[(${`A..N`})]) extends Serializable { | ||
$generatedFunctions | ||
- } | ||
- | ||
|}""" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package mouse | ||
|
||
trait FTupleSyntax extends FTupleNSyntax | ||
|
||
object FTupleSyntax |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe that should be a separate library to prevent this copy-pasting among cats-* libraries. But there is none at the moment.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Like this?
https://github.com/sbt/sbt-boilerplate
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep. I mean that Typelevel could have its own. Because there is an obvious need.