Skip to content

Commit

Permalink
add traverseN to CartesianBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
julien-truffaut committed Sep 30, 2017
1 parent b785cd5 commit add2a1a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
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

0 comments on commit add2a1a

Please sign in to comment.