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

Added examples for Cokleisli #1993

Merged
merged 26 commits into from
Oct 29, 2017
Merged
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
fb60456
Example for beginners to use Arrow composition
Oct 7, 2017
961c1e3
Merge branch 'master' of https://github.com/typelevel/cats
Oct 7, 2017
604b71e
Example for beginners to use Arrow composition
Oct 7, 2017
6f6b09e
Got rid of whitespace at EOL
Oct 8, 2017
4c417aa
Merge branch 'master' of https://github.com/raymondtay/cats
raymondtay Oct 9, 2017
7adc57d
Added type signatures
raymondtay Oct 9, 2017
88abe61
Merge branch 'master' of https://github.com/typelevel/cats
Oct 10, 2017
ba82d30
Merge branch 'master' of https://github.com/raymondtay/cats
Oct 10, 2017
bcbd847
Got rid of the unused import that is borking the build
raymondtay Oct 10, 2017
5942abb
Merge branch 'master' of https://github.com/typelevel/cats
raymondtay Oct 11, 2017
3ce0002
Merge branch 'master' of https://github.com/typelevel/cats
raymondtay Oct 12, 2017
ccab819
Merge branch 'master' of https://github.com/typelevel/cats
raymondtay Oct 16, 2017
5d1a68a
Merge branch 'master' of https://github.com/typelevel/cats
raymondtay Oct 16, 2017
1241d54
Merge branch 'master' of https://github.com/typelevel/cats
raymondtay Oct 18, 2017
64231d5
Merge branch 'master' of https://github.com/raymondtay/cats
raymondtay Oct 18, 2017
185b44e
Merge branch 'master' of https://github.com/typelevel/cats
raymondtay Oct 23, 2017
139c2c4
Merge branch 'master' of https://github.com/raymondtay/cats
raymondtay Oct 23, 2017
e7ee56a
Merge branch 'master' of https://github.com/typelevel/cats
raymondtay Oct 24, 2017
782537f
Added examples for dimap,lmap,rmap for Cokleisli
raymondtay Oct 25, 2017
50dd65e
Exchanged the Cokleisli Id examples with NonEmptyList
raymondtay Oct 27, 2017
e4396bf
Revert "Exchanged the Cokleisli Id examples with NonEmptyList"
raymondtay Oct 27, 2017
5401b18
Exchanged the Cokleisli examples from 'Id' to use 'NonEmptyList' instead
raymondtay Oct 27, 2017
b6cb93d
Merge branch 'master' of https://github.com/typelevel/cats
raymondtay Oct 27, 2017
6a70cea
Removed redundant renderings
raymondtay Oct 28, 2017
517dcea
This is probably safer.
raymondtay Oct 29, 2017
3242791
doh.have not quite woke up yet.
raymondtay Oct 29, 2017
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
32 changes: 32 additions & 0 deletions core/src/main/scala/cats/data/Cokleisli.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,47 @@ import scala.annotation.tailrec
*/
final case class Cokleisli[F[_], A, B](run: F[A] => B) { self =>

/**
* Example:
* {{{
* scala> import cats._, data._
* scala> val f = Cokleisli((xs: NonEmptyList[Int]) => xs.reverse.head)
* scala> def before(x: Double) = x.toInt
* scala> def after(x: Int) = x.toString
* scala> f.dimap(before)(after).run(NonEmptyList.of(1.0,2.0))
* res0: String = 2
* }}}
*/
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._, data._, implicits._
* scala> val f = Cokleisli((xs: NonEmptyList[Int]) => xs.reverse.head)
* scala> def before(x: Double) = x.toInt
* scala> def after(x: Int) = x.toString
* scala> f.lmap(before).rmap(after).run(NonEmptyList.of(1.0,2.0))
* res0: String = 2
* }}}
*/
def lmap[C](f: C => A)(implicit F: Functor[F]): Cokleisli[F, C, B] =
Cokleisli(fc => run(F.map(fc)(f)))

def map[C](f: B => C): Cokleisli[F, A, C] =
Cokleisli(f compose run)

/**
* Example:
* {{{
* scala> import cats._, data._
* scala> val sum = Cokleisli((xs: NonEmptyList[Int]) => xs.reduceLeft(_ + _))
*
* 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