Skip to content

Commit

Permalink
Added examples for dimap,lmap,rmap for Cokleisli
Browse files Browse the repository at this point in the history
  • Loading branch information
raymondtay committed Oct 25, 2017
1 parent e7ee56a commit 782537f
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions core/src/main/scala/cats/data/Cokleisli.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,31 @@ import scala.annotation.tailrec
*/
final case class Cokleisli[F[_], A, B](run: F[A] => B) { self =>

/**
* Example:
* {{{
* scala> import cats.Id, cats.implicits._
* scala> val x : Id[Int] = 42
* scala> def before(x: Int) = x + 1
* scala> def after(x: Int) = x - 1
* scala> val example : Cokleisli[Id,Int,Int] = Cokleisli((f: Id[Int]) => f.extract)
* scala> example.dimap(before)(after) == 42
* }}}
*/
def dimap[C, D](f: C => A)(g: B => D)(implicit F: Functor[F]): Cokleisli[F, C, D] =
Cokleisli(fc => g(run(F.map(fc)(f))))

/**
* Example:
* {{{
* scala> import cats.Id, cats.implicits._
* scala> val x : Id[Int] = 42
* scala> def before(x: Int) = x + 1
* scala> def after(x: Int) = x - 1
* scala> val example : Cokleisli[Id,Int,Int] = Cokleisli((f: Id[Int]) => f.extract)
* scala> example.lmap(before).rmap(after) == 42
* }}}
*/
def lmap[C](f: C => A)(implicit F: Functor[F]): Cokleisli[F, C, B] =
Cokleisli(fc => run(F.map(fc)(f)))

Expand Down

0 comments on commit 782537f

Please sign in to comment.