-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Added flatMapN #4009
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
Added flatMapN #4009
Changes from 2 commits
af65abb
980c53e
6a7ddc0
8631a49
f57146a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -27,6 +27,7 @@ object Boilerplate { | |
| GenSemigroupalBuilders, | ||
| GenSemigroupalArityFunctions, | ||
| GenApplyArityFunctions, | ||
| GenFlatMapArityFunctions, | ||
| GenTupleSemigroupalSyntax, | ||
| GenParallelArityFunctions, | ||
| GenParallelArityFunctions2, | ||
|
|
@@ -285,6 +286,36 @@ object Boilerplate { | |
| } | ||
| } | ||
|
|
||
| object GenFlatMapArityFunctions extends Template { | ||
| def filename(root: File) = root / "cats" / "FlatMapArityFunctions.scala" | ||
| override def range = 2 to maxArity | ||
| 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(", ") | ||
|
|
||
| block""" | ||
| |package cats | ||
| | | ||
| |/** | ||
| | * @groupprio Ungrouped 0 | ||
| | * | ||
| | * @groupname FlatMapArity flatMap arity | ||
| | * @groupdesc FlatMapArity Higher-arity flatMap methods | ||
| | * @groupprio FlatMapArity 999 | ||
| | */ | ||
| |trait FlatMapArityFunctions[F[_]] { self: FlatMap[F] => | ||
| - /** @group MapArity */ | ||
| - def flatMap$arity[${`A..N`}, Z]($fparams)(f: (${`A..N`}) => F[Z]): F[Z] = self.flatten(self.map$arity($fparams)(f)) | ||
| |} | ||
| """ | ||
| } | ||
| } | ||
|
|
||
| final case class ParallelNestedExpansions(arity: Int) { | ||
| val products = (0 until (arity - 2)) | ||
| .foldRight(s"Parallel.parProduct(m${arity - 2}, m${arity - 1})")((i, acc) => s"Parallel.parProduct(m$i, $acc)") | ||
|
|
@@ -516,6 +547,12 @@ object Boilerplate { | |
| else | ||
| s"def traverseN[G[_]: Applicative, Z](f: (${`A..N`}) => G[Z])(implicit traverse: Traverse[F], semigroupal: Semigroupal[F]): G[F[Z]] = Semigroupal.traverse$arity($tupleArgs)(f)" | ||
|
|
||
| val flatMap = | ||
| if (arity == 1) | ||
| s"def flatMap[Z](f: (${`A..N`}) => F[Z])(implicit flatMap: FlatMap[F]): F[Z] = flatMap.flatMap($tupleArgs)(f)" | ||
| else | ||
| s"def flatMapN[Z](f: (${`A..N`}) => F[Z])(implicit flatMap: FlatMap[F]): F[Z] = flatMap.flatMap$arity($tupleArgs)(f)" | ||
|
Comment on lines
+550
to
+554
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So I haven't been able to wrap my head around whether this technically belongs in
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Ah, ok that makes sense :) |
||
|
|
||
|
Comment on lines
+525
to
+555
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looking into the Boilerplate code I noticed, that support for
Shouldn't we follow the same pattern for I am not certain about it though, just an observation.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also there's the Curious, shouldn't we create a
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. All excellent questions! My reason for doing it as it is is the number of methods. The initial concern with regards to adding
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agree, jar size is a concern. Did you try by chance to measure how much the resulting jars get increased in size comparing to the base version? May be it adds not too much...
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I did not actually. I'll give it a shot today/tomorrow and see how it compares. |
||
| block""" | ||
| |package cats | ||
| |package syntax | ||
|
|
@@ -528,6 +565,7 @@ object Boilerplate { | |
| - $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) | ||
|
|
||
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.