Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
3 changes: 2 additions & 1 deletion core/src/main/scala/cats/FlatMap.scala
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ import simulacrum.noop
*
* Must obey the laws defined in cats.laws.FlatMapLaws.
*/
@typeclass trait FlatMap[F[_]] extends Apply[F] {
@typeclass(excludeParents = List("FlatMapArityFunctions"))
trait FlatMap[F[_]] extends Apply[F] with FlatMapArityFunctions[F] {
def flatMap[A, B](fa: F[A])(f: A => F[B]): F[B]

/**
Expand Down
38 changes: 38 additions & 0 deletions project/Boilerplate.scala
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ object Boilerplate {
GenSemigroupalBuilders,
GenSemigroupalArityFunctions,
GenApplyArityFunctions,
GenFlatMapArityFunctions,
GenTupleSemigroupalSyntax,
GenParallelArityFunctions,
GenParallelArityFunctions2,
Expand Down Expand Up @@ -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 */

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- /** @group MapArity */
- /** @group FlatMapArity */

- 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)")
Expand Down Expand Up @@ -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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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 TupleSemigroupalSyntax. It's certainly the easiest place to put it, and it works, I'm just confused by the "semigroupal" in the name :)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Semigroupal captures the idea of composing independent effectful values.

Ah, ok that makes sense :)


Comment on lines +525 to +555

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking into the Boilerplate code I noticed, that support for mapN is currently implemented in this way:

  • SemigroupalArityFunctions contains a bunch of mapX (2 ≤ X ≤ 22) functions (along with other arity functions like imapX and contramapX.
  • These implementations are used then from all other arity syntax, including TupleSemigroupalSyntax.

Shouldn't we follow the same pattern for flatMapN – i.e. add implementations for all flatMapX into SemigroupalArityFunctions in the first place and then make use of it from everywhere else?

I am not certain about it though, just an observation.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also there's the ApplyArityFunctions trait that adds the mapX syntax directly to the Apply typeclass.

Curious, shouldn't we create a FlatMapArityFunctions trait (it does not exist yet) and add flatMapX method directly into the FlatMap typeclass as well?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The 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 .flatMapN was jar size, since adding arity methods adds a lot of code. Because of that I opted to only add it where it's - in my opinion - most useful, which is the tuple syntax. I personally have never used Apply[F].mapN, so I don't know if it's particularly useful to add a FlatMap[F].flatMapN. But if the consensus says otherwise I'm happy to change that.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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...

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The 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
Expand All @@ -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)
Expand Down
6 changes: 5 additions & 1 deletion tests/shared/src/test/scala/cats/tests/SyntaxSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -412,10 +412,14 @@ object SyntaxSuite {
val fa = a.pure[F]
}

def testFlatMap[F[_]: FlatMap, A, B, C, D]: Unit = {
def testFlatMap[F[_]: FlatMap, A, B, C, D, Z]: Unit = {
val a = mock[A]
val returnValue = mock[F[Either[A, B]]]
val done = a.tailRecM[F, B](a => returnValue)
val tfabc = mock[(F[A], F[B], F[C])]
val ff = mock[(A, B, C) => F[Z]]

tfabc.flatMapN(ff)

val x = mock[Function[A, F[B]]]
val y = mock[Function[B, F[C]]]
Expand Down