Skip to content

Commit 08f88ea

Browse files
authored
Merge pull request #4805 from satorg/traverse-foldMap-consistency
Add `traverse` and `foldMap` consistency rule
2 parents 3038737 + 76977f0 commit 08f88ea

File tree

2 files changed

+5
-1
lines changed

2 files changed

+5
-1
lines changed

laws/src/main/scala/cats/laws/TraverseLaws.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,9 @@ trait TraverseLaws[F[_]] extends FunctorLaws[F] with FoldableLaws[F] with Unorde
166166

167167
lhs <-> rhs
168168
}
169+
170+
def traverseConsistentFoldMap[A, B: Monoid](fa: F[A], fn: A => B): IsEq[B] =
171+
F.traverse(fa)(a => Const[B, B](fn(a))).getConst <-> F.foldMap(fa)(fn)
169172
}
170173

171174
object TraverseLaws {

laws/src/main/scala/cats/laws/discipline/TraverseTests.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,8 @@ trait TraverseTests[F[_]] extends FunctorTests[F] with FoldableTests[F] with Uno
8282
"traverse ref zipWithLongIndex" -> forAll(laws.zipWithLongIndexRef[A, C] _),
8383
"traverse ref mapWithLongIndex" -> forAll(laws.mapWithLongIndexRef[A, C] _),
8484
"traverse ref traverseWithLongIndexM" -> forAll(laws.traverseWithLongIndexMRef[Option, A, C] _),
85-
"traverse ref updated" -> forAll(laws.updatedRef[A, A](_, _, _))
85+
"traverse ref updated" -> forAll(laws.updatedRef[A, A](_, _, _)),
86+
"traverse consistent with foldMap" -> forAll(laws.traverseConsistentFoldMap[A, M](_, _))
8687
)
8788
}
8889
}

0 commit comments

Comments
 (0)