Skip to content

Commit

Permalink
Add right and left functor to BiFunctor
Browse files Browse the repository at this point in the history
  • Loading branch information
Luka Jacobowitz committed Aug 23, 2017
1 parent 80a9853 commit 466335e
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions core/src/main/scala/cats/functor/Bifunctor.scala
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ import simulacrum.typeclass
*/
def bimap[A, B, C, D](fab: F[A, B])(f: A => C, g: B => D): F[C, D]

def rightFunctor[X]: Functor[F[X, ?]] =
new RightFunctor[F, X] {val F = self}

def leftFunctor[X]: Functor[F[?, X]] =
new LeftFunctor[F, X] {val F = self}

// derived methods
/**
* apply a function to the "left" functor
Expand Down Expand Up @@ -61,3 +67,17 @@ private[cats] trait ComposedBifunctor[F[_, _], G[_, _]]
F.bimap(fab)(innerBimap, innerBimap)
}
}

private trait LeftFunctor[F[_, _], X] extends Functor[F[?, X]] {
implicit val F: Bifunctor[F]

override def map[A, C](fax: F[A, X])(f: A => C): F[C, X] =
F.bimap(fax)(f, identity)
}

private trait RightFunctor[F[_, _], X] extends Functor[F[X, ?]] {
implicit val F: Bifunctor[F]

override def map[A, C](fxa: F[X, A])(f: A => C): F[X, C] =
F.bimap(fxa)(identity, f)
}

0 comments on commit 466335e

Please sign in to comment.