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 tests to cover derived methods leftMap and rightMap on BiFunctor #652

Merged
merged 2 commits into from
Nov 15, 2015
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
5 changes: 5 additions & 0 deletions core/src/main/scala/cats/syntax/bifunctor.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,10 @@ trait BifunctorSyntax {
}

class BifunctorOps[F[_, _], A, B](fab: F[A, B])(implicit F: Bifunctor[F]) {

def bimap[C, D](f: A => C, g: B => D): F[C,D] = F.bimap(fab)(f,g)

def leftMap[C](f: A => C): F[C, B] = F.leftMap(fab)(f)

def rightMap[D](f: B => D): F[A, D] = F.rightMap(fab)(f)
}
15 changes: 15 additions & 0 deletions laws/src/main/scala/cats/laws/BifunctorLaws.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,21 @@ trait BifunctorLaws[F[_, _]] {
def bifunctorComposition[A, B, C, X, Y, Z](fa: F[A, X], f: A => B, f2: B => C, g: X => Y, g2: Y => Z): IsEq[F[C, Z]] = {
fa.bimap(f, g).bimap(f2, g2) <-> fa.bimap(f andThen f2, g andThen g2)
}

def bifunctorLeftMapIdentity[A, B](fa: F[A, B]): IsEq[F[A, B]] =
fa.leftMap(identity) <-> fa

def bifunctorRightMapIdentity[A, B](fa: F[A, B]): IsEq[F[A, B]] =
fa.rightMap(identity) <-> fa

def bifunctorLeftMapComposition[A, B, C, D](fa: F[A, B], f: A => C, g: C => D): IsEq[F[D, B]] = {
fa.leftMap(f).leftMap(g) <-> fa.leftMap(f andThen g)
}

def bifunctorRightMapComposition[A, B, C, D](fa: F[A, B], f: B => C, g: C => D): IsEq[F[A, D]] = {
fa.rightMap(f).rightMap(g) <-> fa.rightMap(f andThen g)
}

}

object BifunctorLaws {
Expand Down
10 changes: 8 additions & 2 deletions laws/src/main/scala/cats/laws/discipline/BifunctorTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,19 @@ trait BifunctorTests[F[_, _]] extends Laws {
ArbB2: Arbitrary[B => B2],
ArbB3: Arbitrary[B2 => B3],
EqFAB: Eq[F[A, B]],
EqFCZ: Eq[F[A3, B3]]
EqFCZ: Eq[F[A3, B3]],
EqFA3B: Eq[F[A3, B]],
EqFAB3: Eq[F[A, B3]]
): RuleSet = {
new DefaultRuleSet(
name = "Bifunctor",
parent = None,
"Bifunctor Identity" -> forAll(laws.bifunctorIdentity[A, B] _),
"Bifunctor associativity" -> forAll(laws.bifunctorComposition[A, A2, A3, B, B2, B3] _)
"Bifunctor associativity" -> forAll(laws.bifunctorComposition[A, A2, A3, B, B2, B3] _),
"Bifunctor leftMap Identity" -> forAll(laws.bifunctorLeftMapIdentity[A, B] _),
"Bifunctor rightMap Identity" -> forAll(laws.bifunctorRightMapIdentity[A, B] _),
"Bifunctor leftMap associativity" -> forAll(laws.bifunctorLeftMapComposition[A, B, A2, A3] _),
"Bifunctor rightMap associativity" -> forAll(laws.bifunctorRightMapComposition[A, B, B2, B3] _)
)
}
}
Expand Down