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 Traverse1 typeclass (#1577) #1611

Merged
merged 30 commits into from
Jun 19, 2017
Merged
Show file tree
Hide file tree
Changes from 26 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
5452d01
Add Traverse1 typeclass (#1577)
Mar 27, 2017
96d79d2
Add tests and laws for Traverse1 typeclass (#1577)
Apr 18, 2017
4a38545
Merge branch 'master' into master
Apr 22, 2017
3e9afe3
Format with newline at end of file
Apr 22, 2017
7ff5e1e
Replace NonEmptyReducible with NonEmptyTraverse1
May 18, 2017
2e2e7ac
Add Scaladocs to Traverse1
May 18, 2017
82f4537
Remove unneeded Unapply version
May 18, 2017
ab40d79
Readd Reducible instance to OneAnd so a Monad is not needed for a Fol…
May 18, 2017
11e8a5d
Use NonEmptyTraverse1 for traverse1 implementation of OneAnd
May 18, 2017
6945a26
Rename flatSequence
May 28, 2017
17d8d62
Remove NonEmptyReducible class and replace with NonEmptyReducible + T…
May 28, 2017
b1e115b
Replace traverse1 with more performant implementation
May 28, 2017
796ebb4
Merge branch 'master' into master
May 28, 2017
f437a98
Remove Unapply syntax
May 28, 2017
647df06
Separate traverse and traverse1 for OneAnd
May 28, 2017
1f69c30
Override traverse implementation of NonEmptyVector
May 28, 2017
caac5f7
Change type annotation of NonEmptyvector instances to reflect actual …
May 28, 2017
aa5d6aa
Add Law tests for traverse1 instances of NonEmptyList and Vector
May 28, 2017
590ba54
Correct inheritance for OneAnd
May 28, 2017
dfbc064
Add traverse1 law testing for OneAnd instance
May 28, 2017
9093a53
Add doctests for traverse1 and sequence1
May 28, 2017
5c49d9f
Add remaining doctests
May 28, 2017
0cc4531
Override traverse in traverse1 instance of OneAnd
May 28, 2017
a2bbee4
Fix Indentation
May 28, 2017
8e9656b
Remove redundant tests and replace with Traverse1 tests
May 28, 2017
f214c64
Add Traverse1#compose and instance for Nested
May 29, 2017
9ed10ea
Move nested traverse1 instance to NestedInstances
May 29, 2017
7f55edc
Move reducible nested tests
May 29, 2017
02c25d5
rename traverse1 to nonEmptyTraverse
Jun 8, 2017
50cc4c0
Rename intercalate1 to nonEmptyIntercalate
Jun 12, 2017
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 core/src/main/scala/cats/Composed.scala
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,14 @@ private[cats] trait ComposedTraverse[F[_], G[_]] extends Traverse[λ[α => F[G[
F.traverse(fga)(ga => G.traverse(ga)(f))
}

private[cats] trait ComposedTraverse1[F[_], G[_]] extends Traverse1[λ[α => F[G[α]]]] with ComposedTraverse[F, G] with ComposedReducible[F, G] {
def F: Traverse1[F]
def G: Traverse1[G]

override def traverse1[H[_]: Apply, A, B](fga: F[G[A]])(f: A => H[B]): H[F[G[B]]] =
F.traverse1(fga)(ga => G.traverse1(ga)(f))
}

private[cats] trait ComposedTraverseFilter[F[_], G[_]] extends TraverseFilter[λ[α => F[G[α]]]] with ComposedTraverse[F, G] {
def F: Traverse[F]
def G: TraverseFilter[G]
Expand Down
94 changes: 94 additions & 0 deletions core/src/main/scala/cats/Traverse1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package cats

import simulacrum.typeclass

/**
* Traverse1, also known as Traversable1.
*
* `Traverse1` is like a non-empty `Traverse`. In addition to the traverse and sequence
* methods it provides traverse1 and sequence1 methods which require an `Apply` instance instead of `Applicative`.
*/
@typeclass trait Traverse1[F[_]] extends Traverse[F] with Reducible[F] { self =>

/**
* Given a function which returns a G effect, thread this effect
* through the running of this function on all the values in F,
* returning an F[B] in a G context.
*
* Example:
* {{{
* scala> import cats.implicits._
* scala> import cats.data.NonEmptyList
* scala> def countWords(words: List[String]): Map[String, Int] = words.groupBy(identity).mapValues(_.length)
* scala> NonEmptyList.of(List("How", "do", "you", "fly"), List("What", "do", "you", "do")).traverse1(countWords)
* res0: Map[String,cats.data.NonEmptyList[Int]] = Map(do -> NonEmptyList(1, 2), you -> NonEmptyList(1, 1))
* }}}
*/
def traverse1[G[_]: Apply, A, B](fa: F[A])(f: A => G[B]): G[F[B]]

/**
* Thread all the G effects through the F structure to invert the
* structure from F[G[A]] to G[F[A]].
*
* Example:
* {{{
* scala> import cats.implicits._
* scala> import cats.data.NonEmptyList
* scala> val x = NonEmptyList.of(Map("do" -> 1, "you" -> 1), Map("do" -> 2, "you" -> 1))
* scala> val y = NonEmptyList.of(Map("How" -> 3, "do" -> 1, "you" -> 1), Map[String,Int]())
* scala> x.sequence1
* res0: Map[String,NonEmptyList[Int]] = Map(do -> NonEmptyList(1, 2), you -> NonEmptyList(1, 1))
* scala> y.sequence1
* res1: Map[String,NonEmptyList[Int]] = Map()
* }}}
*/
def sequence1[G[_]: Apply, A](fga: F[G[A]]): G[F[A]] =
traverse1(fga)(identity)
Copy link
Contributor

Choose a reason for hiding this comment

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

This method is untested.



/**
* A traverse1 followed by flattening the inner result.
*
* Example:
* {{{
* scala> import cats.implicits._
* scala> import cats.data.NonEmptyList
* scala> def countWords(words: List[String]): Map[String, Int] = words.groupBy(identity).mapValues(_.length)
* scala> val x = NonEmptyList.of(List("How", "do", "you", "fly"), List("What", "do", "you", "do"))
* scala> x.flatTraverse1(_.groupByNel(identity))
* res0: Map[String,cats.data.NonEmptyList[String]] = Map(do -> NonEmptyList(do, do, do), you -> NonEmptyList(you, you))
* }}}
*/
def flatTraverse1[G[_], A, B](fa: F[A])(f: A => G[F[B]])(implicit G: Apply[G], F: FlatMap[F]): G[F[B]] =
G.map(traverse1(fa)(f))(F.flatten)
Copy link
Contributor

@edmundnoble edmundnoble Apr 29, 2017

Choose a reason for hiding this comment

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

Ditto (untested) for this, and flatSequence, sequence, traverse1U, sequence1U in the same class.


/**
* Thread all the G effects through the F structure and flatten to invert the
* structure from F[G[F[A]]] to G[F[A]].
*
* Example:
* {{{
* scala> import cats.implicits._
* scala> import cats.data.NonEmptyList
* scala> val x = NonEmptyList.of(Map(0 ->NonEmptyList.of(1, 2)), Map(0 -> NonEmptyList.of(3)))
* scala> val y: NonEmptyList[Map[Int, NonEmptyList[Int]]] = NonEmptyList.of(Map(), Map(1 -> NonEmptyList.of(3)))
* scala> x.flatSequence1
* res0: Map[Int,cats.data.NonEmptyList[Int]] = Map(0 -> NonEmptyList(1, 2, 3))
* scala> y.flatSequence1
* res1: Map[Int,cats.data.NonEmptyList[Int]] = Map()
* }}}
*/
def flatSequence1[G[_], A](fgfa: F[G[F[A]]])(implicit G: Apply[G], F: FlatMap[F]): G[F[A]] =
G.map(traverse1(fgfa)(identity))(F.flatten)

override def traverse[G[_] : Applicative, A, B](fa: F[A])(f: (A) => G[B]): G[F[B]] =
traverse1(fa)(f)

def compose[G[_]: Traverse1]: Traverse1[λ[α => F[G[α]]]] =
new ComposedTraverse1[F, G] {
val F = self
val G = Traverse1[G]
}


}
16 changes: 15 additions & 1 deletion core/src/main/scala/cats/data/Nested.scala
Original file line number Diff line number Diff line change
Expand Up @@ -133,13 +133,20 @@ private[data] sealed abstract class NestedInstances9 extends NestedInstances10 {
}
}

private[data] sealed abstract class NestedInstances10 {
private[data] sealed abstract class NestedInstances10 extends NestedInstances11 {
implicit def catsDataInvariantForNestedContravariant[F[_]: Invariant, G[_]: Contravariant]: Invariant[Nested[F, G, ?]] =
new NestedInvariant[F, G] {
val FG: Invariant[λ[α => F[G[α]]]] = Invariant[F].composeContravariant[G]
}
}

private[data] sealed abstract class NestedInstances11 {
Copy link
Member Author

Choose a reason for hiding this comment

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

Not sure if making a new class here was the best thing to do, feedback very welcome :)

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think you can put the catsDataTraverse1ForNested inside NestedInstances.

As you can never have a TraverseFilter and a Traverse1 for a data type at the same time, I don't think that the composition Traverse and TraverseFilter versus the composition of Traverse1 and Traverse1 can be ambiguous.

implicit def catsDataTraverse1ForNested[F[_]: Traverse1, G[_]: Traverse1]: Traverse1[Nested[F, G, ?]] =
new NestedTraverse1[F, G] {
val FG: Traverse1[λ[α => F[G[α]]]] = Traverse1[F].compose[G]
}
}

private[data] trait NestedInvariant[F[_], G[_]] extends Invariant[Nested[F, G, ?]] {
def FG: Invariant[λ[α => F[G[α]]]]

Expand Down Expand Up @@ -233,6 +240,13 @@ private[data] trait NestedReducible[F[_], G[_]] extends Reducible[Nested[F, G, ?
FG.reduceRightTo(fga.value)(f)(g)
}

private[data] trait NestedTraverse1[F[_], G[_]] extends Traverse1[Nested[F, G, ?]] with NestedTraverse[F, G] with NestedReducible[F, G] {
def FG: Traverse1[λ[α => F[G[α]]]]

override def traverse1[H[_]: Apply, A, B](fga: Nested[F, G, A])(f: A => H[B]): H[Nested[F, G, B]] =
Apply[H].map(FG.traverse1(fga.value)(f))(Nested(_))
}

private[data] trait NestedContravariant[F[_], G[_]] extends Contravariant[Nested[F, G, ?]] {
def FG: Contravariant[λ[α => F[G[α]]]]

Expand Down
14 changes: 11 additions & 3 deletions core/src/main/scala/cats/data/NonEmptyList.scala
Original file line number Diff line number Diff line change
Expand Up @@ -365,9 +365,9 @@ object NonEmptyList extends NonEmptyListInstances {
private[data] sealed trait NonEmptyListInstances extends NonEmptyListInstances0 {

implicit val catsDataInstancesForNonEmptyList: SemigroupK[NonEmptyList] with Reducible[NonEmptyList]
with Comonad[NonEmptyList] with Traverse[NonEmptyList] with Monad[NonEmptyList] =
with Comonad[NonEmptyList] with Traverse1[NonEmptyList] with Monad[NonEmptyList] =
new NonEmptyReducible[NonEmptyList, List] with SemigroupK[NonEmptyList] with Comonad[NonEmptyList]
with Traverse[NonEmptyList] with Monad[NonEmptyList] {
with Monad[NonEmptyList] with Traverse1[NonEmptyList] {

def combineK[A](a: NonEmptyList[A], b: NonEmptyList[A]): NonEmptyList[A] =
a concat b
Expand All @@ -394,7 +394,15 @@ private[data] sealed trait NonEmptyListInstances extends NonEmptyListInstances0

def extract[A](fa: NonEmptyList[A]): A = fa.head

def traverse[G[_], A, B](fa: NonEmptyList[A])(f: A => G[B])(implicit G: Applicative[G]): G[NonEmptyList[B]] =
def traverse1[G[_], A, B](nel: NonEmptyList[A])(f: A => G[B])(implicit G: Apply[G]): G[NonEmptyList[B]] =
Foldable[List].reduceRightToOption[A, G[List[B]]](nel.tail)(a => G.map(f(a))(_ :: Nil)) { (a, lglb) =>
G.map2Eval(f(a), lglb)(_ :: _)
}.map {
case None => G.map(f(nel.head))(NonEmptyList(_, Nil))
case Some(gtail) => G.map2(f(nel.head), gtail)(NonEmptyList(_, _))
}.value

override def traverse[G[_], A, B](fa: NonEmptyList[A])(f: A => G[B])(implicit G: Applicative[G]): G[NonEmptyList[B]] =
fa traverse f

override def foldLeft[A, B](fa: NonEmptyList[A], b: B)(f: (B, A) => B): B =
Expand Down
14 changes: 11 additions & 3 deletions core/src/main/scala/cats/data/NonEmptyVector.scala
Original file line number Diff line number Diff line change
Expand Up @@ -189,9 +189,9 @@ final class NonEmptyVector[+A] private (val toVector: Vector[A]) extends AnyVal
private[data] sealed trait NonEmptyVectorInstances {

implicit val catsDataInstancesForNonEmptyVector: SemigroupK[NonEmptyVector] with Reducible[NonEmptyVector]
with Comonad[NonEmptyVector] with Traverse[NonEmptyVector] with Monad[NonEmptyVector] =
with Comonad[NonEmptyVector] with Traverse1[NonEmptyVector] with Monad[NonEmptyVector] =
new NonEmptyReducible[NonEmptyVector, Vector] with SemigroupK[NonEmptyVector] with Comonad[NonEmptyVector]
with Traverse[NonEmptyVector] with Monad[NonEmptyVector] {
with Monad[NonEmptyVector] with Traverse1[NonEmptyVector] {

def combineK[A](a: NonEmptyVector[A], b: NonEmptyVector[A]): NonEmptyVector[A] =
a concatNev b
Expand Down Expand Up @@ -226,7 +226,15 @@ private[data] sealed trait NonEmptyVectorInstances {

def extract[A](fa: NonEmptyVector[A]): A = fa.head

def traverse[G[_], A, B](fa: NonEmptyVector[A])(f: (A) => G[B])(implicit G: Applicative[G]): G[NonEmptyVector[B]] =
def traverse1[G[_], A, B](nel: NonEmptyVector[A])(f: A => G[B])(implicit G: Apply[G]): G[NonEmptyVector[B]] =
Foldable[Vector].reduceRightToOption[A, G[Vector[B]]](nel.tail)(a => G.map(f(a))(_ +: Vector.empty)) { (a, lglb) =>
G.map2Eval(f(a), lglb)(_ +: _)
}.map {
case None => G.map(f(nel.head))(NonEmptyVector(_, Vector.empty))
case Some(gtail) => G.map2(f(nel.head), gtail)(NonEmptyVector(_, _))
}.value

override def traverse[G[_], A, B](fa: NonEmptyVector[A])(f: (A) => G[B])(implicit G: Applicative[G]): G[NonEmptyVector[B]] =
G.map2Eval(f(fa.head), Always(Traverse[Vector].traverse(fa.tail)(f)))(NonEmptyVector(_, _)).value

override def foldLeft[A, B](fa: NonEmptyVector[A], b: B)(f: (B, A) => B): B =
Expand Down
24 changes: 23 additions & 1 deletion core/src/main/scala/cats/data/OneAnd.scala
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ final case class OneAnd[F[_], A](head: A, tail: F[A]) {
def forall(p: A => Boolean)(implicit F: Foldable[F]): Boolean =
p(head) && F.forall(tail)(p)


def reduceLeft(f: (A, A) => A)(implicit F: Foldable[F]): A =
F.foldLeft(tail, head)(f)

/**
* Left-associative fold on the structure using f.
*/
Expand Down Expand Up @@ -94,7 +98,7 @@ final case class OneAnd[F[_], A](head: A, tail: F[A]) {
s"OneAnd(${A.show(head)}, ${FA.show(tail)})"
}

private[data] sealed trait OneAndInstances extends OneAndLowPriority2 {
private[data] sealed trait OneAndInstances extends OneAndLowPriority3 {

implicit def catsDataEqForOneAnd[A, F[_]](implicit A: Eq[A], FA: Eq[F[A]]): Eq[OneAnd[F, A]] =
new Eq[OneAnd[F, A]]{
Expand Down Expand Up @@ -221,4 +225,22 @@ private[data] trait OneAndLowPriority2 extends OneAndLowPriority1 {
}
}

private[data] trait OneAndLowPriority3 extends OneAndLowPriority2 {
implicit def catsDataTraverse1ForOneAnd[F[_]](implicit F: Traverse[F], F2: MonadCombine[F]): Traverse1[OneAnd[F, ?]] =
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can we override traverse using the implementation originally used in Traverse?
We probably want to override the traverse for NonEmptyVector as well using the previous implementation, come to think of it.

This implementation using MonadCombine also means that you cannot get a Traverse[OneAnd[F, ?]] instance anymore if F has a Traverse but no MonadCombine.

Copy link
Member Author

Choose a reason for hiding this comment

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

Great idea!

new NonEmptyReducible[OneAnd[F, ?], F] with Traverse1[OneAnd[F, ?]] {
def traverse1[G[_], A, B](fa: OneAnd[F, A])(f: (A) => G[B])(implicit G: Apply[G]): G[OneAnd[F, B]] = {
import cats.syntax.cartesian._

fa.map(a => Apply[G].map(f(a))(OneAnd(_, F2.empty[B])))(F)
.reduceLeft(((acc, a) => (acc |@| a).map((x: OneAnd[F, B], y: OneAnd[F, B]) => x.combine(y))))
}

Copy link
Collaborator

Choose a reason for hiding this comment

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

Can you add the implementation of traverse used in the Traverse instance above here as well?

override def traverse[G[_], A, B](fa: OneAnd[F, A])(f: (A) => G[B])(implicit G: Applicative[G]): G[OneAnd[F, B]] = {
G.map2Eval(f(fa.head), Always(F.traverse(fa.tail)(f)))(OneAnd(_, _)).value
}

def split[A](fa: OneAnd[F, A]): (A, F[A]) = (fa.head, fa.tail)
}
}

object OneAnd extends OneAndInstances
6 changes: 3 additions & 3 deletions core/src/main/scala/cats/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ package object cats {
* encodes pure unary function application.
*/
type Id[A] = A
implicit val catsInstancesForId: Bimonad[Id] with Monad[Id] with Traverse[Id] with Reducible[Id] =
new Bimonad[Id] with Monad[Id] with Traverse[Id] with Reducible[Id] {
implicit val catsInstancesForId: Bimonad[Id] with Monad[Id] with Traverse1[Id] =
new Bimonad[Id] with Monad[Id] with Traverse1[Id] {
def pure[A](a: A): A = a
def extract[A](a: A): A = a
def flatMap[A, B](a: A)(f: A => B): B = f(a)
Expand All @@ -51,7 +51,7 @@ package object cats {
def foldLeft[A, B](a: A, b: B)(f: (B, A) => B) = f(b, a)
def foldRight[A, B](a: A, lb: Eval[B])(f: (A, Eval[B]) => Eval[B]): Eval[B] =
f(a, lb)
def traverse[G[_], A, B](a: A)(f: A => G[B])(implicit G: Applicative[G]): G[B] =
def traverse1[G[_], A, B](a: A)(f: A => G[B])(implicit G: Apply[G]): G[B] =
f(a)
override def foldMap[A, B](fa: Id[A])(f: A => B)(implicit B: Monoid[B]): B = f(fa)
override def reduce[A](fa: Id[A])(implicit A: Semigroup[A]): A =
Expand Down
1 change: 1 addition & 0 deletions core/src/main/scala/cats/syntax/all.scala
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ trait AllSyntax
with StrongSyntax
with TraverseFilterSyntax
with TraverseSyntax
with Traverse1Syntax
with TupleSyntax
with ValidatedSyntax
with VectorSyntax
Expand Down
1 change: 1 addition & 0 deletions core/src/main/scala/cats/syntax/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ package object syntax {
object strong extends StrongSyntax
object monadTrans extends MonadTransSyntax
object traverse extends TraverseSyntax
object traverse1 extends Traverse1Syntax
object traverseFilter extends TraverseFilterSyntax
object tuple extends TupleSyntax
object validated extends ValidatedSyntax
Expand Down
2 changes: 2 additions & 0 deletions core/src/main/scala/cats/syntax/reducible.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cats
package syntax


trait ReducibleSyntax extends Reducible.ToReducibleOps {
implicit final def catsSyntaxNestedReducible[F[_]: Reducible, G[_], A](fga: F[G[A]]): NestedReducibleOps[F, G, A] =
new NestedReducibleOps[F, G, A](fga)
Expand All @@ -9,3 +10,4 @@ trait ReducibleSyntax extends Reducible.ToReducibleOps {
final class NestedReducibleOps[F[_], G[_], A](val fga: F[G[A]]) extends AnyVal {
def reduceK(implicit F: Reducible[F], G: SemigroupK[G]): G[A] = F.reduceK(fga)
}

4 changes: 4 additions & 0 deletions core/src/main/scala/cats/syntax/traverse1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package cats
package syntax

trait Traverse1Syntax extends Traverse1.ToTraverse1Ops
73 changes: 73 additions & 0 deletions laws/src/main/scala/cats/laws/Traverse1Laws.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package cats.laws


import cats.{Apply, Id, Semigroup, Traverse1}
import cats.data.{Const, Nested}
import cats.syntax.traverse1._
import cats.syntax.reducible._

trait Traverse1Laws[F[_]] extends TraverseLaws[F] with ReducibleLaws[F] {
Copy link
Contributor

Choose a reason for hiding this comment

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

None of the laws and test were actually hit. We need to add one law test in a data type that has a vanilla Traverse1 instance and one for a data type that uses the NonEmptyTraverse1.

implicit override def F: Traverse1[F]

def traverse1Identity[A, B](fa: F[A], f: A => B): IsEq[F[B]] = {
fa.traverse1[Id, B](f) <-> F.map(fa)(f)
}

def traverse1SequentialComposition[A, B, C, M[_], N[_]](
fa: F[A],
f: A => M[B],
g: B => N[C]
)(implicit
N: Apply[N],
M: Apply[M]
): IsEq[Nested[M, N, F[C]]] = {

val lhs = Nested(M.map(fa.traverse1(f))(fb => fb.traverse1(g)))
val rhs = fa.traverse1[Nested[M, N, ?], C](a => Nested(M.map(f(a))(g)))
lhs <-> rhs
}

def traverse1ParallelComposition[A, B, M[_], N[_]](
fa: F[A],
f: A => M[B],
g: A => N[B]
)(implicit
N: Apply[N],
M: Apply[M]
): IsEq[(M[F[B]], N[F[B]])] = {
type MN[Z] = (M[Z], N[Z])
implicit val MN = new Apply[MN] {
def ap[X, Y](f: MN[X => Y])(fa: MN[X]): MN[Y] = {
val (fam, fan) = fa
val (fm, fn) = f
(M.ap(fm)(fam), N.ap(fn)(fan))
}
override def map[X, Y](fx: MN[X])(f: X => Y): MN[Y] = {
val (mx, nx) = fx
(M.map(mx)(f), N.map(nx)(f))
}
override def product[X, Y](fx: MN[X], fy: MN[Y]): MN[(X, Y)] = {
val (mx, nx) = fx
val (my, ny) = fy
(M.product(mx, my), N.product(nx, ny))
}
}
val lhs: MN[F[B]] = fa.traverse1[MN, B](a => (f(a), g(a)))
val rhs: MN[F[B]] = (fa.traverse1(f), fa.traverse1(g))
lhs <-> rhs
}

def reduceMapDerived[A, B](
fa: F[A],
f: A => B
)(implicit B: Semigroup[B]): IsEq[B] = {
val lhs: B = fa.traverse1[Const[B, ?], B](a => Const(f(a))).getConst
val rhs: B = fa.reduceMap(f)
lhs <-> rhs
}
}

object Traverse1Laws {
def apply[F[_]](implicit ev: Traverse1[F]): Traverse1Laws[F] =
new Traverse1Laws[F] { def F: Traverse1[F] = ev }
}
Loading