Skip to content

Commit

Permalink
Add law for foldable
Browse files Browse the repository at this point in the history
  • Loading branch information
Luka Jacobowitz committed Oct 29, 2017
1 parent 4e6456a commit 8745b55
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 4 deletions.
4 changes: 4 additions & 0 deletions laws/src/main/scala/cats/laws/FoldableLaws.scala
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,10 @@ trait FoldableLaws[F[_]] {
F.dropWhile_(fa)(p) <-> F.foldLeft(fa, mutable.ListBuffer.empty[A]) { (buf, a) =>
if (buf.nonEmpty || !p(a)) buf += a else buf
}.toList

def orderedConsistency[A: Eq](x: F[A], y: F[A])(implicit ev: Eq[F[A]]): IsEq[List[A]] =
if (x === y) (F.toList(x) <-> F.toList(y))
else List.empty[A] <-> List.empty[A]
}

object FoldableLaws {
Expand Down
5 changes: 1 addition & 4 deletions laws/src/main/scala/cats/laws/TraverseLaws.scala
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ trait TraverseLaws[F[_]] extends FunctorLaws[F] with FoldableLaws[F] {
lhs <-> rhs
}

def traverseOrderConsistent[A](fa: F[A]) = {
def traverseOrderConsistent[A](fa: F[A]): IsEq[Option[A]] = {
class FirstOption[T](val o: Option[T])

implicit val firstOptionMonoid = new Monoid[FirstOption[A]] {
Expand All @@ -84,10 +84,7 @@ trait TraverseLaws[F[_]] extends FunctorLaws[F] with FoldableLaws[F] {
)(store).getConst.o

first <-> traverseFirst


}

def mapWithIndexRef[A, B](fa: F[A], f: (A, Int) => B): IsEq[F[B]] = {
val lhs = F.mapWithIndex(fa)(f)
val rhs = F.traverse(fa)(a =>
Expand Down
2 changes: 2 additions & 0 deletions laws/src/main/scala/cats/laws/discipline/FoldableTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ trait FoldableTests[F[_]] extends Laws {
CogenA: Cogen[A],
CogenB: Cogen[B],
EqA: Eq[A],
EqFA: Eq[F[A]],
EqB: Eq[B],
EqOptionA: Eq[Option[A]]
): RuleSet = {
Expand All @@ -26,6 +27,7 @@ trait FoldableTests[F[_]] extends Laws {
parent = None,
"foldLeft consistent with foldMap" -> forAll(laws.leftFoldConsistentWithFoldMap[A, B] _),
"foldRight consistent with foldMap" -> forAll(laws.rightFoldConsistentWithFoldMap[A, B] _),
"ordered constistency" -> forAll(laws.orderedConsistency[A] _),
"exists consistent with find" -> forAll(laws.existsConsistentWithFind[A] _),
"forall consistent with exists" -> forAll(laws.forallConsistentWithExists[A] _),
"forall true if empty" -> forAll(laws.forallEmpty[A] _),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ trait ReducibleTests[F[_]] extends FoldableTests[F] {
EqG: Eq[G[Unit]],
EqA: Eq[A],
EqB: Eq[B],
EqFA: Eq[F[A]],
EqOptionA: Eq[Option[A]],
MonoidA: Monoid[A],
MonoidB: Monoid[B]
Expand Down

0 comments on commit 8745b55

Please sign in to comment.