Skip to content

Commit

Permalink
Exchanged the Cokleisli Id examples with NonEmptyList
Browse files Browse the repository at this point in the history
  • Loading branch information
raymondtay committed Oct 27, 2017
1 parent 782537f commit 50dd65e
Showing 1 changed file with 24 additions and 12 deletions.
36 changes: 24 additions & 12 deletions core/src/main/scala/cats/data/Cokleisli.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ 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
* scala> import cats.Id, cats.data.NonEmptyList, cats.implicits._
* scala> val f = Cokleisli((xs: NonEmptyList[Int]) => xs.reverse.head)
* f: cats.data.Cokleisli[cats.data.NonEmptyList,Int,Int] = Cokleisli(<function1>)
* scala> def before(x: String) = x.toInt
* scala> def after(x: Int) = x.toString
* scala> f.dimap(before)(after).run(NonEmptyList.of("1","2"))
* res0: String = 2
* }}}
*/
def dimap[C, D](f: C => A)(g: B => D)(implicit F: Functor[F]): Cokleisli[F, C, D] =
Expand All @@ -28,12 +29,13 @@ 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.lmap(before).rmap(after) == 42
* scala> import cats.Id, cats.data.NonEmptyList, cats.implicits._
* scala> val f = Cokleisli((xs: NonEmptyList[Int]) => xs.reverse.head)
* f: cats.data.Cokleisli[cats.data.NonEmptyList,Int,Int] = Cokleisli(<function1>)
* scala> def before(x: String) = x.toInt
* scala> def after(x: Int) = x.toString
* scala> f.lmap(before).rmap(after).run(NonEmptyList.of("1","2"))
* res0: String = 2
* }}}
*/
def lmap[C](f: C => A)(implicit F: Functor[F]): Cokleisli[F, C, B] =
Expand All @@ -42,6 +44,16 @@ final case class Cokleisli[F[_], A, B](run: F[A] => B) { self =>
def map[C](f: B => C): Cokleisli[F, A, C] =
Cokleisli(f compose run)

/**
* Example:
* {{{
* scala> val sum = Cokleisli((xs: NonEmptyList[Int]) => xs.reduceLeft(_ + _))
* sum: cats.data.Cokleisli[cats.data.NonEmptyList,Int,Int] = Cokleisli(<function1>)
*
* scala> sum.contramapValue((xs: NonEmptyList[String]) => xs.map(_.toInt)).run(NonEmptyList.of("1","2","3"))
* res4: Int = 6
* }}}
*/
def contramapValue[C](f: F[C] => F[A]): Cokleisli[F, C, B] =
Cokleisli(run compose f)

Expand Down

0 comments on commit 50dd65e

Please sign in to comment.