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 traverseN to cartesian syntax #1942

Merged
merged 1 commit into from
Sep 30, 2017
Merged
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
8 changes: 8 additions & 0 deletions project/Boilerplate.scala
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,8 @@ object Boilerplate {
- invariant.imap($nestedProducts) { case ${`nested (a..n)`} => f(${`a..n`}) } { z => val ${`(a..n)`} = g(z); ${`nested (a..n)`} }
- def tuple$arity[F[_], ${`A..N`}]($fparams)(implicit cartesian: Cartesian[F], invariant: functor.Invariant[F]):F[(${`A..N`})] =
- imap$arity($fargsS)((${`_.._`}))(identity)
- def traverse$arity[F[_], G[_], ${`A..N`}, Z]($fparams)(f: (${`A..N`}) => G[Z])(implicit cartesian: Cartesian[F], traverse: Traverse[F], applicative: Applicative[G]): G[F[Z]] =
- traverse.traverse($nestedProducts) { case ${`nested (a..n)`} => f(${`a..n`}) }
|}
"""
}
Expand Down Expand Up @@ -255,6 +257,11 @@ object Boilerplate {
""
}

val traverse =
if (arity == 1) s"def traverse[G[_]: Applicative, Z](f: (${`A..N`}) => G[Z])(implicit traverse: Traverse[F]): G[F[Z]] = traverse.traverse($tupleArgs)(f)"
else s"def traverseN[G[_]: Applicative, Z](f: (${`A..N`}) => G[Z])(implicit traverse: Traverse[F], cartesian: Cartesian[F]): G[F[Z]] = Cartesian.traverse$arity($tupleArgs)(f)"


block"""
|package cats
|package syntax
Expand All @@ -270,6 +277,7 @@ object Boilerplate {
- $contramap
- $imap
- $tupled
- $traverse
- def apWith[Z](f: F[(${`A..N`}) => Z])(implicit apply: Apply[F]): F[Z] = apply.ap$n(f)($tupleArgs)
-}
|
Expand Down
10 changes: 9 additions & 1 deletion tests/src/test/scala/cats/tests/SyntaxTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,11 @@ object SyntaxTests extends AllInstances with AllSyntax {
val as2: List[A] = fa.dropWhile_(f5)
}

def testTraverse[F[_]: Traverse: FlatMap, G[_]: Applicative, A, B]: Unit = {
def testTraverse[F[_]: Traverse: FlatMap, G[_]: Applicative, A, B, C, Z]: Unit = {
val tfabc = mock[(F[A], F[B], F[C])]
val fa = mock[F[A]]
val fb = mock[F[B]]
val fc = mock[F[C]]
val f1 = mock[A => G[B]]
val gfb: G[F[B]] = fa.traverse(f1)

Expand All @@ -137,6 +140,11 @@ object SyntaxTests extends AllInstances with AllSyntax {

val fga = mock[F[G[A]]]
val gunit: G[F[A]] = fga.sequence

val ft = mock[(A, B, C) => G[Z]]

val gfabc = tfabc traverseN ft
val gfabc2 = (fa, fb, fc) traverseN ft
}


Expand Down