From 782537f0813d401160cf7c14c1ba8df34425ba25 Mon Sep 17 00:00:00 2001 From: Raymond Tay Date: Wed, 25 Oct 2017 10:46:50 +0800 Subject: [PATCH] Added examples for dimap,lmap,rmap for Cokleisli --- core/src/main/scala/cats/data/Cokleisli.scala | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/core/src/main/scala/cats/data/Cokleisli.scala b/core/src/main/scala/cats/data/Cokleisli.scala index a21bf77216..0fafeaf42f 100644 --- a/core/src/main/scala/cats/data/Cokleisli.scala +++ b/core/src/main/scala/cats/data/Cokleisli.scala @@ -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)))